Skip to content

Commit

Permalink
feat: speed up first connection when using asyncio (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 12, 2023
1 parent 7c527b4 commit 0b6ba93
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/dbus_fast/aio/message_bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@
NO_REPLY_EXPECTED_VALUE = MessageFlag.NO_REPLY_EXPECTED.value


def _generate_hello_serialized(next_serial: int) -> bytes:
return Message(
destination="org.freedesktop.DBus",
path="/org/freedesktop/DBus",
interface="org.freedesktop.DBus",
member="Hello",
serial=next_serial,
)._marshall(False)


HELLO_1_SERIALIZED = _generate_hello_serialized(1)


def _future_set_exception(fut: asyncio.Future, exc: Exception) -> None:
if fut is not None and not fut.done():
fut.set_exception(exc)
Expand Down Expand Up @@ -234,18 +247,14 @@ def on_hello(reply, err):
self.disconnect()
self._finalize(err)

hello_msg = Message(
destination="org.freedesktop.DBus",
path="/org/freedesktop/DBus",
interface="org.freedesktop.DBus",
member="Hello",
serial=self.next_serial(),
)

self._method_return_handlers[hello_msg.serial] = on_hello
self._stream.write(hello_msg._marshall(False))
next_serial = self.next_serial()
self._method_return_handlers[next_serial] = on_hello
if next_serial == 1:
serialized = HELLO_1_SERIALIZED
else:
serialized = _generate_hello_serialized(next_serial)
self._stream.write(serialized)
self._stream.flush()

return await future

async def introspect(
Expand Down

0 comments on commit 0b6ba93

Please sign in to comment.