[prev in list] [next in list] [prev in thread] [next in thread] 

List:       freedesktop-dbus
Subject:    [python-dbus/PyQt] Message loops in separate threads
From:       dwelch91 <dwelch91 () gmail ! com>
Date:       2007-12-17 3:30:19
Message-ID: 5c9a36e80712161930p1179c374lb353e516547d5ab5 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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

[Attachment #5 (text/html)]

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&#39;s my naive attempt at this (that doesn&#39;t work). I guess \
I don&#39;t understand enough about the message loops to know whether this is \
completely misguided or not... \
<br><br>Thanks,<br><br>Don<br><br><br>----------------------------------------<br>#! \
/usr/bin/env python<br><br>import sys, threading<br>import Queue<br><br>import \
dbus<br>import dbus.service<br>from dbus.mainloop.glib import DBusGMainLoop <br>from \
gobject import MainLoop<br><br>from PyQt4 import QtGui, QtCore<br>from PyQt4.QtCore \
import Qt<br><br><br>&nbsp;&nbsp;&nbsp; <br>class \
Test(dbus.service.Object):<br>&nbsp;&nbsp;&nbsp; def __init__(self,update_queue, \
name, object_path):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  \
dbus.service.Object.__init__(self, name, object_path)<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; self.update_queue = update_queue<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; \
@dbus.service.method(&#39;com.hplip.StatusService&#39;)<br>&nbsp;&nbsp;&nbsp; def \
EchoString(self, s):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return s \
<br>&nbsp;&nbsp;&nbsp; <br><br>class \
DBusThread(threading.Thread):<br>&nbsp;&nbsp;&nbsp; def __init__(self, \
update_queue):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
threading.Thread.__init__(self)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
self.update_queue = update_queue<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
<br>&nbsp;&nbsp;&nbsp; def run(self):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
dbus_loop = DBusGMainLoop(set_as_default=False) <br><br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; self.system_bus = \
dbus.SystemBus(mainloop=dbus_loop)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
self.session_bus = dbus.SessionBus(mainloop=dbus_loop)<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
self.system_bus.add_signal_receiver(self.handle_system_signal, \
sender_keyword=&#39;sender&#39;,  <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; destination_keyword=&#39;dest&#39;, \
interface_keyword=&#39;interface&#39;, <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; member_keyword=&#39;member&#39;, \
path_keyword=&#39;path&#39;)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.session_name = \
dbus.service.BusName (&quot;com.hplip.StatusService&quot;, \
self.session_bus)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; self.test_object = \
Test(self.update_queue, self.session_name, \
&quot;/com/hplip/StatusService/Test&quot;)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; \
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print &quot;Running dbus loop...&quot; \
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; MainLoop().run()<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; print &quot;dbus loop exit.&quot;<br><br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; def \
handle_system_signal(*args,**kwds):<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print \
args<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print kwds<br>&nbsp;&nbsp;&nbsp; \
&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>def main(args): \
<br>&nbsp;&nbsp;&nbsp; app = QtGui.QApplication(sys.argv)<br><br>&nbsp;&nbsp;&nbsp; \
tray_icon = QtGui.QSystemTrayIcon()<br>&nbsp;&nbsp;&nbsp; icon = \
QtGui.QIcon(&quot;HPmenu.png&quot;) <br>&nbsp;&nbsp;&nbsp; \
tray_icon.setIcon(icon)<br>&nbsp;&nbsp;&nbsp; menu = \
QtGui.QMenu()<br>&nbsp;&nbsp;&nbsp; menu.addAction (&quot;&amp;Quit&quot;, quit, \
Qt.CTRL + Qt.Key_Q)<br>&nbsp;&nbsp;&nbsp; \
tray_icon.setContextMenu(menu)<br><br>&nbsp;&nbsp;&nbsp; \
tray_icon.show()<br><br>&nbsp;&nbsp;&nbsp; update_queue = Queue.Queue() # unfinished \
<br><br>&nbsp;&nbsp;&nbsp; dbus_thread = DBusThread(update_queue)<br> \
&nbsp;&nbsp;&nbsp; dbus_thread.start()<br><br>&nbsp;&nbsp;&nbsp; print &quot;Running \
Qt mainloop...&quot;<br>&nbsp;&nbsp;&nbsp; app.exec_()<br>&nbsp;&nbsp;&nbsp; print \
&quot;qt loop exit&quot;<br><br><br>if __name__ == \
&#39;__main__&#39;:<br>&nbsp;&nbsp;&nbsp; sys.exit(main(sys.argv))<br>



_______________________________________________
dbus mailing list
dbus@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dbus


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic