[PyQt] QThread not forcibly terminating as expected
apaku at gmx.de
Tue Mar 6 19:46:17 GMT 2012
On 06.03.12 11:10:19, Brian Knudson wrote:
> > Message: 4
> > Date: Tue, 6 Mar 2012 08:54:09 +0100
> > From: Andreas Pakulat <apaku at gmx.de>
> > To: pyqt at riverbankcomputing.com
> > Subject: Re: [PyQt] QThread not forcibly terminating as expected
> > Message-ID: <20120306075409.GA30346 at barmbek>
> > Content-Type: text/plain; charset=us-ascii
> > On 05.03.12 13:35:02, Brian Knudson wrote:
> >> On Mar 3, 2012, at 4:00 AM, pyqt-request at riverbankcomputing.com wrote:
> >>> Message: 1
> >>> Date: Sat, 3 Mar 2012 10:17:39 +0100
> >>> From: Andreas Pakulat <apaku at gmx.de>
> >>> To: pyqt at riverbankcomputing.com
> >>> Subject: Re: [PyQt] QThread not forcibly terminating as expected
> >>> Message-ID: <20120303091739.GA8274 at trinity.apaku.dnsalias.org>
> >>> Content-Type: text/plain; charset=us-ascii
> >>> Can you show some sample code for this, I don't quite understand what
> >>> you mean with "its not updatable" or "its locked"?
> >> As is often the case, while making my example, I found the solution to one of my problems... I'm using only one thread for this widget, so while it's busy, the widget can't be updated. Using multiple threads will fix this - to a degree. Please read on.
> > Ah, so with the example you provided there would've been only 1 qthread
> > object and you re-used it? Thats indeed not a good idea, each thread
> > should run only once, so you should create a new object each time you
> > want to run something in a thread.
> Good, common sense advice. Thanks. Is there any harm in reusing a list of threads (as I've done in the example)? Is it bad practice? (I'm somewhat new to this much threading, if you hadn't noticed)
I never tried re-starting a QThread, but I believe Qt does not really
support that. If you want to start a thread, create a new object and
start it. Once its terminated or finished, throw it away.
Usually its better to use QThreadPool or the Qt Concurrent framework
(not sure if thats wrapped in PyQt).
> >> I've made a small example: http://churchofbk.com/misc/qthread_example.py.
> >> In this example, I've replicated the way I'm doing things in my application so it may not be the ideal way of doing things for this small of an example, but it works. My question is still related to the fact that threads don't seem to stop executing when I tell them to.
> > The reason that the thread is not terminating in the code above is that
> > you're using self.quit(). If you look in the API docs, you'll notice
> > that quit merely ends the event-loop of the QThread. But in your example
> > you never start the event-loop in the threads, so there's nothing to
> > quit. Changing that to use self.terminate() makes the example code work
> > as expected, the label is not updated for PushButton 2.
> > If thats not the case for you your Qt and PyQt versions might be
> > interesting, maybe its a bug in either that got fixed.
> I'd tried that, (as you can see by the fact that it was commented out in the code), but was getting very unexpected results in an earlier incarnation of the example - the application was crashing... not just throwing-an-exception crashing, but an OS level crash. However, trying it again, as it sits now, seems to work just fine. Hmm. I'll go with terminate() & keep an eye on it.
Crashes in PyQt are most often either accessing a QObject thats been
deleted from the C++ side already, not keeping a reference to an
Python-created object after handing it to Qt api (which doesn't keep a
reference to the Python-parts and hence the underlying C++ object gets
deleted behind the scenes) or bugs in Qt.
More information about the PyQt