[PyQt] question about the QThread event loop

melkor at orangepalantir.org melkor at orangepalantir.org
Fri Feb 12 12:29:10 GMT 2010


I have also addressed this issue lately, and I found get an object to
process signals in a different event loop, you have to create that object
in the event loop you wish to use it.

I have written up an example using this here:

http://orangepalantir.org/topicspace/index.php?idnum=53&comments=on

I never got any feedback so I don't know if this is good or not.  In your
example all three of your threads are QObjects created in the main event
loop, and therefore their signal/slots are evaluated as such.

I found similar behavior in both c++ and python.


mbs


> Hi, I have a question about the QThread event loop.
>
> In order to test the behavior of QThread event loop, I tested the
> following code.
> Each of thread A, B, C has its own event loop.
> Thread B sends a signal to thread A, and the thread C sends a signal
> to the thread B,
> after the event loops start.
>
> =====================================================================
> import time, sys
> from PyQt4 import QtCore
> from PyQt4 import QtGui
>
> app = QtGui.QApplication(sys.argv)
>
> class MyThread(QtCore.QThread):
>
>     def __init__(self, name, sec):
>         super(MyThread, self).__init__()
>         self.setObjectName(name)
>         self.sec = sec
>         self.dict_handler = {}
>
>     def run(self):
>         print "'" + self.objectName() + "' is starting in",
> QtCore.QThread.currentThreadId()
>         time.sleep(self.sec)
>         self.emit(QtCore.SIGNAL("mysignal"))
>         self.exec_()
>
>     def myhandler(self):
>         print "'" + self.objectName() + "' is handling in",
> QtCore.QThread.currentThreadId()
>
> a = MyThread('a', 0)
> b = MyThread('b', 2)
> c = MyThread('c', 4)
>
> a.connect(b, QtCore.SIGNAL("mysignal"), a.myhandler,
> QtCore.Qt.QueuedConnection)
> b.connect(c, QtCore.SIGNAL("mysignal"), b.myhandler,
> QtCore.Qt.QueuedConnection)
>
> a.start()
> b.start()
> c.start()
>
> app.exec_()
> =====================================================================
>
>
> and the result was:
> =====================================================================
> 'a' is starting in <sip.voidptr object at 0x01AB1530>             => line
> 1
> 'b' is starting in <sip.voidptr object at 0x01AB1530>             => line
> 2
> 'c' is starting in <sip.voidptr object at 0x01AB1530>             => line
> 3
> 'a' is handling in <sip.voidptr object at 0x01AB1530>             => line
> 4
> 'b' is handling in <sip.voidptr object at 0x01AB1530>             => line
> 5
> =====================================================================
>
> I expected the threads in line 1, 2, 3. the main thread, however, I
> thought that
> the threads in line 4 and 5 must be different from the main thread and
> each other.
>
> 'Why all the handlers were executed in the same thread?
>
> I will appreciate if anyone answers this question.
> Thanks.
>
>
> ------------------------------
>
> Message: 5
> Date: Fri, 12 Feb 2010 03:21:53 -0600
> From: Russell Valentine <russ at coldstonelabs.org>
> To: Doh-Hyoung Kim <kim.dohhyoung at gmail.com>
> Cc: pyqt at riverbankcomputing.com
> Subject: Re: [PyQt] question about the QThread event loop
> Message-ID: <4B751DB1.2090002 at coldstonelabs.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Doh-Hyoung Kim wrote:
>> I expected the threads in line 1, 2, 3. the main thread, however, I
>> thought that
>> the threads in line 4 and 5 must be different from the main thread and
>> each other.
>>
>> 'Why all the handlers were executed in the same thread?
>>
>> I will appreciate if anyone answers this question.
>> Thanks.
>
>
> http://doc.trolltech.com/4.5/qthread.html#currentThreadId
> Warning: The handle returned by this function is used for internal
> purposes and should not be used in any application code. On Windows, the
> returned value is a pseudo-handle for the current thread that cannot be
> used for numerical comparison.
>
>
> Running this same code gives me a entirly different result. More of what
> I expected, but I think this goes to show that the warning should be
> taken. Mainly signals and slots are handled in the main thread. While
> the first three messages are in their own thread.
>
> 'a' is starting in 140649595894096
> 'b' is starting in 140649587501392
> 'c' is starting in 140649579108688
> 'a' is handling in 140649768179440
> 'b' is handling in 140649768179440
>
>
> The idea is you can do processing in Qthread, then emit signals to
> update the GUI. The run function of QThread is what the new thread is
> running, and you update the GUI thread by emiting signals. So I would
> expect the 'starting' message each to be from a different thread and all
> the 'handling' messages to be from the main GUI thread.
>
> By chaning currentThreadId() to currentThread() you should be able to
> see this. My output is below:
>
> 'a' is starting in <__main__.MyThread object at 0x7a31e8>
> 'c' is starting in <__main__.MyThread object at 0x7a32f8>
> 'b' is starting in <__main__.MyThread object at 0x7a3270>
> 'a' is handling in <PyQt4.QtCore.QThread object at 0x7a3380>
> 'b' is handling in <PyQt4.QtCore.QThread object at 0x7a3380>
>
>
> Russell Valentine
>
>
> ------------------------------
>
> _______________________________________________
> PyQt mailing list
> PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
> End of PyQt Digest, Vol 67, Issue 18
> ************************************
>




More information about the PyQt mailing list