dbus-fast/examples/aio-list-names.py
David Lechner d71f0e1fbd
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.
2022-09-26 12:50:22 -05:00

33 lines
669 B
Python
Executable File

#!/usr/bin/env python3
import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/.."))
import asyncio
import json
from dbus_fast import Message, MessageType
from dbus_fast.aio import MessageBus
async def main():
bus = await MessageBus().connect()
reply = await bus.call(
Message(
destination="org.freedesktop.DBus",
path="/org/freedesktop/DBus",
interface="org.freedesktop.DBus",
member="ListNames",
)
)
if reply.message_type == MessageType.ERROR:
raise Exception(reply.body[0])
print(json.dumps(reply.body[0], indent=2))
asyncio.run(main())