[PyQt] Multithreading and plotting

Fabien Lafont lafont.fabien at gmail.com
Wed Jan 16 09:25:54 GMT 2013


Hey,

I found a solution! I've created the thread in the main:

if __name__ == "__main__":
    qApp = QtGui.QApplication([" "])
    aw = ApplicationWindow()
    aw.showMaximized()
    thread = AThread()
    sys.exit(qApp.exec_())

But I "run" thread.start() only when clicking on my button. I'll try to
post my code soon here to have your comments on it. I'm physicist so I'm
not coding very well :) but It's better if the code is well structured and
not only if it's working (as we love to do as physicist). Thanks again!

Fabien


2013/1/15 David Hoese <dhoese at gmail.com>

> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130116/f57cfee8/attachment-0001.html>


More information about the PyQt mailing list