[python-dbus/PyQt] Message loops in separate threads

dwelch91 dwelch91 at gmail.com
Sun Dec 16 19:30:19 PST 2007


So, as a follow up to the PyQt/python-dbus issue connecting to both the
session and system buses, should it be possible to place each messsge loop
(Glib and PyQt) in a separate thread? Here's my naive attempt at this (that
doesn't work). I guess I don't understand enough about the message loops to
know whether this is completely misguided or not...

Thanks,

Don


----------------------------------------
#! /usr/bin/env python

import sys, threading
import Queue

import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
from gobject import MainLoop

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt



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

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


class DBusThread(threading.Thread):
    def __init__(self, update_queue):
        threading.Thread.__init__(self)
        self.update_queue = update_queue

    def run(self):
        dbus_loop = DBusGMainLoop(set_as_default=False)

        self.system_bus = dbus.SystemBus(mainloop=dbus_loop)
        self.session_bus = dbus.SessionBus(mainloop=dbus_loop)

        self.system_bus.add_signal_receiver(self.handle_system_signal,
sender_keyword='sender',
            destination_keyword='dest', interface_keyword='interface',
            member_keyword='member', path_keyword='path')

        self.session_name = dbus.service.BusName("com.hplip.StatusService",
self.session_bus)
        self.test_object = Test(self.update_queue, self.session_name,
"/com/hplip/StatusService/Test")

        print "Running dbus loop..."
        MainLoop().run()
        print "dbus loop exit."


    def handle_system_signal(*args,**kwds):
        print args
        print kwds


def main(args):
    app = QtGui.QApplication(sys.argv)

    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()

    update_queue = Queue.Queue() # unfinished

    dbus_thread = DBusThread(update_queue)
    dbus_thread.start()

    print "Running Qt mainloop..."
    app.exec_()
    print "qt loop exit"


if __name__ == '__main__':
    sys.exit(main(sys.argv))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20071216/764c2004/attachment.html 


More information about the dbus mailing list