System DBus does not recognize mainloop (Python)

Simon McVittie simon.mcvittie at collabora.co.uk
Tue Mar 5 02:30:15 PST 2013


On 04/03/13 21:23, Alexander wrote:
>     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
>
>     mainLoop = gobject.MainLoop()
>
>     threading.Thread(target=mainLoop.run).start()
>     dbus_system = dbus.SystemBus(mainloop=mainLoop)

That last line is wrong: the SystemBus constructor expects a dbus-python
main-loop-integration wrapper, not a GObject.MainLoop (I realise the
terminology is pretty confusing). Either:

      dbus_system = dbus.SystemBus()

(this works because you said set_as_default=True), or:

      dbus_loop = dbus.mainloop.glib.DBusGMainLoop()

      mainLoop = gobject.MainLoop()

      dbus_system = dbus.SystemBus(mainloop=dbus_loop)

(Or you can even set_as_default=True *and* explicitly pass in dbus_loop,
if you want.)

I notice that you're iterating mainLoop in a non-main thread. I'm not
sure that the parts of dbus-glib used for DBusGMainLoop are sufficiently
thread-safe to do that - you might get hard-to-debug crashes if you mix
D-Bus accesses in the main thread, and in the thread that is iterating
mainLoop.

If this is a GUI or other event-driven GLib application, you don't need
the other thread: you can just run mainLoop in the main thread, and
you'll get D-Bus events mixed in with your GUI/network/other events.

Alternatively, if you want a thread-safe D-Bus binding using GLib, you
can use GDBus via gobject-introspection (part of gi.repository.Gio).

    S


More information about the dbus mailing list