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

dwelch91 dwelch91 at gmail.com
Sat Dec 15 22:37:32 PST 2007


Hello,

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.

Scenario #1: Setup session bus first, and system bus second. In this
scenario, the system bus handler get signals, the session bus exports the
object, but a viewer like qdbusviewer hangs when it tries to open the path
to the object. It seems that the call to dbus.SystemBus() "kills" the
session bus setup (and if I remove the add_signal_receiver() call, and leave
just the system_bus = dbus.SystemBus() call, it still fails).

session_bus = dbus.SessionBus()
name = dbus.service.BusName("com.hplip.StatusService", session_bus)
test = Test(name, "/com/hplip/StatusService/Test")

system_bus = dbus.SystemBus()

# processes of non-privledged users can rec. signals from the system bus
system_bus.add_signal_receiver(handle_signal, sender_keyword='sender',
    destination_keyword='dest', interface_keyword='interface',
    member_keyword='member', path_keyword='path')


Scenario #2: Setup system bus first, and the session bus second. In this
scenario, the object exported on the session bus is callable (I can
successfully call the Echo method in qdbusviewer), but the signals from the
system bus no longer function. It seems that the call to dbus.SessionBus()
"kills" the system bus setup (and if I remove the next 2 lines and leave
just the session_bus = dbus.SessionBus() line, it still fails).

system_bus = dbus.SystemBus()

# processes of non-privledged users can rec. signals from the system bus
system_bus.add_signal_receiver(handle_signal, sender_keyword='sender',
    destination_keyword='dest', interface_keyword='interface',
    member_keyword='member', path_keyword='path')

session_bus = dbus.SessionBus()
name = dbus.service.BusName("com.hplip.StatusService", session_bus)
test = Test(name, "/com/hplip/StatusService/Test")

The complete script is here (in scenario #1 order):

#! /usr/bin/env python

import dbus
import dbus.service
import dbus.mainloop.qt
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt
import sys


def handle_signal(*args, **kwds):
    print "Signal!"
    print kwds
    print
    tray_icon.showMessage("HPLIP Status", QtCore.QString("Received message
from %1").arg(kwds['sender']))


def quit():
    app.quit()

class Test(dbus.service.Object):
    def __init__(self, name, object_path):
        dbus.service.Object.__init__(self, name, object_path)

    @dbus.service.method('com.hplip.StatusService')
    def EchoString(self, s):
        return s


app = QtGui.QApplication(sys.argv)
dbus.mainloop.qt.DBusQtMainLoop(set_as_default=True)


session_bus = dbus.SessionBus()
name = dbus.service.BusName("com.hplip.StatusService", session_bus)
test = Test(name, "/com/hplip/StatusService/Test")


system_bus = dbus.SystemBus()

# processes of non-privledged users can call methods on the system bus
proxy = system_bus.get_object('org.freedesktop.DBus',
'/org/freedesktop/DBus')
print proxy.GetId()


# processes of non-privledged users can rec. signals from the system bus
system_bus.add_signal_receiver(handle_signal, sender_keyword='sender',
    destination_keyword='dest', interface_keyword='interface',
    member_keyword='member', path_keyword='path')


tray_icon = QtGui.QSystemTrayIcon()
icon = QtGui.QIcon("HPmenu.png")
tray_icon.setIcon(icon)
menu = QtGui.QMenu()
menu.addAction("&Quit", quit, Qt.CTRL + Qt.Key_Q)
tray_icon.setContextMenu(menu)

tray_icon.show()
app.exec_()



I don't know if there is something wrong in my code, if there is a bug in
dbus or the qt dbus mainloop, or I am making some other sort of rookie
mistake. It does say in the python-dbus docs that: "Of course, you can
connect to both [system and session buses] in the same application.", so I
am pretty sure it is possible.

Thanks for the help,

Don
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20071215/6ca568c7/attachment.html 


More information about the dbus mailing list