use asyncio.run() in examples

calling `asyncio.get_running_loop()` will fail if there is no running event loop, so we should use `asyncio.run()` instead to create a new loop.

Also, use events for infinite waiting instead of futures since there is no return value.
This commit is contained in:
David Lechner 2022-09-26 12:50:22 -05:00 committed by GitHub
parent 691ff8e1bf
commit d71f0e1fbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 11 additions and 23 deletions

View File

@ -78,8 +78,6 @@ from dbus_fast.aio import MessageBus
import asyncio
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus().connect()
@ -106,9 +104,9 @@ async def main():
properties.on_properties_changed(on_properties_changed)
await loop.create_future()
await asyncio.Event().wait()
loop.run_until_complete(main())
asyncio.run(main())
```
## The Service Interface
@ -159,9 +157,9 @@ async def main():
# now that we are ready to handle requests, we can request name from D-Bus
await bus.request_name('test.name')
# wait indefinitely
await asyncio.get_running_loop().create_future()
await asyncio.Event().wait()
asyncio.get_running_loop().run_until_complete(main())
asyncio.run(main())
```
## The Low-Level Interface
@ -177,8 +175,6 @@ from dbus_fast.aio import MessageBus
import asyncio
import json
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus().connect()
@ -195,7 +191,7 @@ async def main():
print(json.dumps(reply.body[0], indent=2))
loop.run_until_complete(main())
asyncio.run(main())
```
## Projects that use python-dbus-fast

View File

@ -90,4 +90,4 @@ If any file descriptors are sent or received (DBus type ``h``), the variable ref
await bus.wait_for_disconnect()
asyncio.get_running_loop().run_until_complete(main())
asyncio.run(main())

View File

@ -10,8 +10,6 @@ import json
from dbus_fast import Message, MessageType
from dbus_fast.aio import MessageBus
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus().connect()
@ -31,4 +29,4 @@ async def main():
print(json.dumps(reply.body[0], indent=2))
loop.run_until_complete(main())
asyncio.run(main())

View File

@ -17,8 +17,6 @@ import asyncio
from dbus_fast.aio import MessageBus
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus(bus_address="tcp:host=127.0.0.1,port=55556").connect()
@ -34,4 +32,4 @@ async def main():
)
loop.run_until_complete(main())
asyncio.run(main())

View File

@ -101,8 +101,6 @@ else:
if not signature:
exit_error("--signature is a required argument when passing a message body")
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus(bus_type=bus_type).connect()
@ -135,4 +133,4 @@ async def main():
sys.exit(ret)
loop.run_until_complete(main())
asyncio.run(main())

View File

@ -64,4 +64,4 @@ async def main():
await bus.wait_for_disconnect()
asyncio.get_running_loop().run_until_complete(main())
asyncio.run(main())

View File

@ -8,8 +8,6 @@ import asyncio
from dbus_fast.aio import MessageBus
loop = asyncio.get_running_loop()
async def main():
bus = await MessageBus().connect()
@ -45,4 +43,4 @@ async def main():
await bus.wait_for_disconnect()
loop.run_until_complete(main())
asyncio.run(main())