[PyQt] QThread not forcibly terminating as expected

Brian Knudson briank at pipelinefx.com
Tue Mar 6 19:10:19 GMT 2012


> 
> 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)

>>> Can you leave out the network-system from your app, replace it with some
>>> kind of busy-loop, i.e. 
>>> 
>>> while(True):
>>> time.sleep(.5)
>>> 
>>> Does that still lock up the widget until you terminate the thread? If so
>>> please post a minimal example.
>> 
>> 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.

Thanks for all your help.  This has been a good exercise.

-Brian


More information about the PyQt mailing list