[python-dbus] Cannot connect to both system and session buses in same program

Simon McVittie simon.mcvittie at collabora.co.uk
Sun Dec 16 05:40:07 PST 2007


On Sat, 15 Dec 2007 at 22:37:32 -0800, dwelch91 wrote:
> I'm playing around with a little test dbus/PyQt4 python script, and have run
> into an issue... I am trying to accomplish two things in this script: 1)
> receive signals from the system bus, and 2) export a callable object on the
> session bus. When I run my little test script, only the last of these 2
> things that I setup work.

Using the GLib main loop I'm able to receive signals from both buses at the
same time; using the Qt main loop I'm not. It sounds as though the Qt D-Bus
main loop adaptor (which is in fact part of pyqt) doesn't support more than
one bus at the same time. Please file this as a bug against PyQt. It may
help to attach my signal listener code, which is about the simplest
possible test case for this.

Signal listener:

---->8----
#!/usr/bin/python

import sys
from dbus import SessionBus, SystemBus
from dbus.mainloop.glib import DBusGMainLoop
from gobject import MainLoop
# for Qt: from dbus.mainloop.qt import DBusQtMainLoop
# for Qt: from PyQt4.QtGui import QApplication


DBusGMainLoop(set_as_default=True)
# for Qt: app = QApplication(sys.argv)
# for Qt: DBusQtMainLoop(set_as_default=True)

def sys_handle_signal(*args, **kwargs):
    print "SYS: %r : %r" % (kwargs, args)

def sess_handle_signal(*args, **kwargs):
    print "SESS: %r : %r" % (kwargs, args)

sessbus = SessionBus()
sysbus = SystemBus()

sessbus.add_signal_receiver(sess_handle_signal, sender_keyword='sender',
    destination_keyword='dest', interface_keyword='iface',
    member_keyword='member', path_keyword='path')

sysbus.add_signal_receiver(sys_handle_signal, sender_keyword='sender',
    destination_keyword='dest', interface_keyword='iface',
    member_keyword='member', path_keyword='path')

MainLoop().run()
# for Qt: app.exec_()
---->8----

Signal emitter:

---->8----
#!/usr/bin/python
from dbus import SessionBus

SessionBus().send_message(SignalMessage('/', 'com.example.Iface', 'Signal'))
---->8----

System bus "signal emitter" (you'll get two NameOwnerChanged signals every
time this runs, which is enough to indicate that your main loop is
dispatching the system bus - alternatively, do something else on the
system bus, like connecting to a wireless network with NetworkManager
and watching for DeviceStrengthChanged signals)

---->8----
#!/usr/bin/python
from dbus import SystemBus

SystemBus()
---->8----

    Simon


More information about the dbus mailing list