[PyKDE] Re: Threading problem with PyQt4 and Macosx

Phil Thompson phil at riverbankcomputing.co.uk
Sun Feb 25 17:10:37 GMT 2007


On Sunday 25 February 2007 4:19 pm, Phil Thompson wrote:
> On Sunday 25 February 2007 3:00 pm, Michael Guntsche wrote:
> > On Feb 25, 2007, at 15:12, Phil Thompson wrote:
> > > Not to my knowledge.
> > >
> > > Phil
> >
> > So both examples here are working on your system then?
>
> No - but my knowledge doesn't tell me why not. However, I've just had an
> idea...
>
> A proxy QObject is created for the Python callable slot. The thread
> affinity of the proxy will be the thread that the connect() is executed in.
>
> If the connect is done in your __init__() method the underlying events are
> sent to the main thread's event loop which you are processing and
> everything works.
>
> If the connect is done in your run() method the underlying events are sent
> to the sub-thread where you don't have an event loop running.
>
> It seems that I need to work out how to find the thread affinity of a
> Python object...

...which doesn't seem possible. So I think it's a case of clarifying the 
documentation...

If you connect to a SLOT() then the slot is executed in the thread as 
described in the Qt documentation.

If you connect to a Python callable then the slot is executed according to the 
same rules with the additional proviso that a SLOT is created on the fly in 
the thread in which the call to connect() is made.

You can then get your sample-thread example working by either moving the 
connect() call to the __init__() method, or by making the connection a direct 
one (or an auto one). In the first case the slot will execute in the main 
thread, and in the second it will execute in the sub-thread. As the slot is 
updating the GUI then you want to do the former.

Matt - does this explanation still mean that the GIL needs to be released in 
QObject ctors etc? Or have I got 2 separate issues confused?

Phil




More information about the PyQt mailing list