[PyQt] question about cyclic references with QThread
Arnold Krille
arnold at arnoldarts.de
Fri Mar 27 14:33:38 GMT 2009
Hi,
On Friday 27 March 2009 15:14:07 Darren Dale wrote:
> I have a simple question about QThread. The attached simple example
> illustrates a case where I have a widget that creates and holds a reference
> to a thread, setting itself as the parent. I need the parent to hold a
> reference to the thread, so I can interrupt its execution for example. I
> want to delete the thread when it finishes executing.
> What the script demonstrates is that when the widget catches the thread's
> finished signal and deletes its reference to the thread, the thread
> continues to persist until the widget itself goes out of scope. Is this due
> to a cyclic reference? Can anyone suggest what I have done wrong, how I can
> improve it?
Its not a cyclic reference, just a double reference. While you set your home-
made reference self.thread to be None, each QObject has a refernce to all its
childs. So you would have to "reparent" the thread to have 0 (or None) as the
parent to be able to garbage-collect it. Or initialize your MyThread with None
as parent instead of self.
BTW: I think (I haven't actually used threads in python yet), that if your
custom thread only runs python, you would be better of to use pythons own
threading classes. Or run the "heavy-work" in a slot that keeps scheduling
itself via QTimer::singleShot until everything is done. The reason: Python has
a big lock (at least up to 2.6) so only one thread runs python code at a time.
Which in your example means that either your thread is running or the main-
part, but not both in parallel (which you probably intended). To make use of
multiple cores, you would have to do the heavy work either in an own process
(which can run its own python interpreter) or implement it as a native C++
QThread and import it via sip...
Have fun,
Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090327/cd2fa983/attachment.bin
More information about the PyQt
mailing list