altdesktop / python-dbus-next

🚌 The next great DBus library for Python with asyncio support
https://python-dbus-next.readthedocs.io/en/latest/
MIT License
187 stars 59 forks source link

Expose connection state #74

Closed hetelek closed 3 years ago

hetelek commented 3 years ago

Problem: Currently, there is no variable or method to instantaneously check whether or not the dbus connection is alive. There is only bus.wait_for_disconnect(), which will wait for a disconnect rather than immediately return whether or not the connection is active.

Workaround: One way to do this with the current release is bus._disconnect_future.done(), but this requires a private instance variable.

Proposal: Expose a public variable or method that allows a user to check if the bus is connected. For example, bus.is_connected():

def is_connected() -> bool:
    return not self._disconnect_future.done()
acrisci commented 3 years ago

I can see how this would be useful, but in general I think it's better to try to run the command and catch failures and keep track of the state yourself on events.

I would accept a connected instance variable on the message bus that's kept up to date with the events which indicates we believe we can send and receive messages. Keep in mind you also need to initialize in a disconnected state and set this to true after the Hello reply is received.