[PyQt] Multithreading and plotting

David Hoese dhoese at gmail.com
Tue Jan 15 18:38:11 GMT 2013


On 1/15/13 9:46 AM, Fabien Lafont wrote:
> I've changed the program according to your remarks. It works fine! I 
> have only one problem. I want to start the new thread when I clik on a 
> QPushButton but If I just remove
>
>     thread = AThread()
>     thread.start()
>
> from
>
> if __name__ == "__main__":
>     qApp = QtGui.QApplication([" "])
>     aw = ApplicationWindow()
>     aw.showMaximized()
>     thread = AThread()
>     thread.start()
>     sys.exit(qApp.exec_())
>
>
> and I create a single button :
>
> def demarrer(self):
>       thread = AThread()
>       thread.start()
>
>
> The program crashes and "say":    QThread: Destroyed while thread is 
> still running
>
>
> Do you have any idea?
Without trying this myself, I'm guessing what's happening is that the 
thread object is getting destroyed/garbage collected after the 
"demarrer" function is finished.  When that function gets called the 
"thread = AThread()" line creates the "AThread" instance and assigns it 
to the local variable name "thread", which is a handle to the actual OS 
level thread.  When the function exits that local variable "thread" does 
not exist anymore and will eventually be garbage collected.  This is 
probably where the error message is coming from because the QThread 
object ("thread") is destroyed (garbage collected) while the low-level 
thread is still running.

My suggestion, as a starting point, would be to create a class instance 
attribute by doing "self.child_thread = AThread()".  This way when the 
function exists there is still a pointer to the QThread object.  Don't 
forget you still need to kill the thread off before the application 
finishes, otherwise you'll probably still get this error after 
everything closes.  I think you were using a qApp.exit signal for that 
before.  Hope this helps.

-Dave



More information about the PyQt mailing list