[PyQt] question about the QThread event loop

Russell Valentine russ at coldstonelabs.org
Fri Feb 12 09:21:53 GMT 2010


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


More information about the PyQt mailing list