[PyQt] Best way to pass commands between threads?

Giovanni Bajo rasky at develer.com
Tue Mar 17 19:30:22 GMT 2009


On 3/17/2009 8:22 PM, Marc Nations wrote:
> 
> 
> On Tue, Mar 17, 2009 at 11:53 AM, Phil Thompson 
> <phil at riverbankcomputing.com <mailto:phil at riverbankcomputing.com>> wrote:
> 
>     On Tue, 17 Mar 2009 11:32:48 -0500, Marc Nations
>     <mnations.lists at gmail.com <mailto:mnations.lists at gmail.com>>
>     wrote:
>      > Hi,
>      > In the past with other gui toolkits I've set up a periodic call
>     function
>      > that pulls control back from the child threads and take care of
>     all the
>     gui
>      > business that gets originated in the child threads. Either I've
>     stored
>     all
>      > the commands in a queue to be processed when periodic check is
>     executed
>     or
>      > used some other technique so that the gui runs the commands from
>     within
>     the
>      > gui thread and doesn't spit the bit by trying to perform the
>     commands in
>      > another thread.
>      >
>      > Does Qt have a "best accepted" way of handling this? I've read
>     that some
>      > use
>      > connections to pass control between threads, but I wasn't sure if
>     that's
>      > the
>      > most efficient way. I basically just need to output text updates
>     to the
>      > status window, so I can either queue up the text to be displayed
>     or the
>      > calls themselves to be executed later. However if Qt efficiently
>     fires a
>      > signal when a certain event happens and fills the slot in the
>     appropriate
>      > thread then that sounds like a better approach.
> 
>     Qt only has one mechanism for communications between threads - events.
>     Inter-thread connections are implemented using events.
> 
>     Use whatever results in the most readable and/or reusable code.
>     Personally
>     I'd use signals and slots with the text as an argument, so that the code
>     could be reused without threads without being changed.
> 
>     Phil
> 
> 
> So will using the threading functionality in Python, independent of the 
> threading functionality in Qt, cause problems with the execution? 

Yes, it will.

> I created a worker thread using the standard Python threading module, so 
> as a result I can't use it to emit a signal. I can create another class 
> in that thread (basically a dummy class from QtCore.QObject) and use it 
> to just be a signal emitter. That's the quickest fix. But if not using 
> the threading functionality inherent in Qt will cause issues down the 
> road, then I'll need to rewrite my thread class.

Yes, I think you should rewrite it to use a QThread.
-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com


More information about the PyQt mailing list