[PyQt] Threads with PyQt. Qt's or Python's?
Giovanni Bajo
rasky at develer.com
Mon Jan 19 15:00:39 GMT 2009
On 1/19/2009 3:13 PM, eliben wrote:
> I've seen various references to this issue before, but nothing to fully
> address it as I'd expect.
>
> Can you comment on the pros and cons of using QThread vs Python's threads
> with PyQt?
>
> I'll begin: on the surface, Python's threads make more sense because they're
> Qt independent and can be ported between apps that don't necessarily depend
> on Qt (for example a web version of a GUI app). The Python thread API is
> powerful enough for all uses, it seems.
>
> However, there are concerns. Perhaps QThreads are more efficient? Or maybe
> more tightly integrated with the other parts of PyQt, so it's easier to use
> them?
It's mostly the same. The main difference is that QThreads are better
integrated with Qt (asynchrnous signals/slots, event loop, etc.). Also,
you can't use Qt from a Python thread (you can't for instance post event
to the main thread through QApplication.postEvent): you need a QThread
for that to work.
A general rule of thumb might be to use QThreads if you're going to
interact somehow with Qt, and use Python threads otherwise. Though I
find it difficult to imagine a thread *not* interacting with Qt at all...
BTW: remember that, with Qt, you don't need threads for many tasks that
are sometimes solved with threads (a prominent example is network
programming). So you might want to think if you really need threads in
the first place.
> Are there differences with regards to the GIL (are QThreads controlled
> by Python's interpreter too?)
It's the same. When Python code is in execution, the GIL is held, so the
threads are basically mutually exclusive.
--
Giovanni Bajo
Develer S.r.l.
http://www.develer.com
More information about the PyQt
mailing list