LEW21 / pydbus

Pythonic DBus library
GNU Lesser General Public License v2.1
327 stars 76 forks source link

Watching bus names #31

Closed ghost closed 7 years ago

ghost commented 7 years ago

Is there something like g_bus_watch_name() in pydbus to notice if a service has stopped or has been restarted? If the service has stopped due to w.e. reason, I want the client to notice that and try to "reconnect", i.e. getting the proxy object again, so I don't have to restart it.

Another way I found is connecting to the NameOwnerChanged signal on org.freedesktop.DBus and check if the owner of my service exists or not.

ghost commented 7 years ago

Ok the way with NameOwnerChanged is pretty simple and works pretty well so far with the existing api of pydbus.

Example:

from gi.repository import GLib
from pydbus import SessionBus

def onNameOwnerChanged(self, serviceName, oldOwner, newOwner):
  if serviceName == "my.personal.service":
    if not newOwner: # service offline
      print("Connection to my personal service lost.")
    if newOwner: # service (re)started, (re)connect
      print("Connected to my personal service.")

bus = SessionBus()
bus.get("org.freedesktop.DBus").NameOwnerChanged.connect(self.onNameOwnerChanged)
print("Connecting to my personal service...")
try:
  myservice = bus.get("my.personal.service")
  print("Connected to my personal service.")
except GLib.GError as err: # assuming the service is running in a glib event loop
  print("Connecting failed. My personal service is offline.")

So implementing something like the g_bus_watch_name() is not needed imho.