Killing threads

bieffe62 at bieffe62 at
Mon Apr 6 09:45:47 CEST 2009

On 6 Apr, 05:25, ericwoodwo... at wrote:
> On Apr 5, 11:07 pm, Dennis Lee Bieber <wlfr... at> wrote:
> > On Sun, 5 Apr 2009 17:27:15 -0700 (PDT), imageguy
> > <imageguy1... at> declaimed the following in
> > gmane.comp.python.general:
> > > In threading.Event python 2.5 docs say;
> > > "This is one of the simplest mechanisms for communication between
> > > threads: one thread signals an event and other threads wait for it. "
> > > Again, I have limited experience, however, in my reading of the
> > > threading manual and review examples, Events were specifically design
> > > to be a thread safe way to communicate a 'state' to running threads ?
> > > In the OP's example 'do stuff' was open to wide interpretation,
> > > however, if within the thread's main 'while' loop the tread checks to
> > > see if the 'keepgoing' Event.isSet(), in what scenario would this
> > > create deadlock ?
> >         If you are going to perform a CPU intensive polling loop, there is
> > no sense in using the Event system in the first place... Just create a
> > globally accessible flag and set it to true when you want to signal the
> > threads (or false if you don't want to use the negation "while not
> > flagged: do next processing step")
> >         Event is optimized for the case wherein threads can WAIT (block) on
> > the Event object.
> > --
> >         Wulfraed        Dennis Lee Bieber               KD6MOG
> >         wlfr... at             wulfr... at
> >                 HTTP://
> >         (Bestiaria Support Staff:               web-a... at
> >                 HTTP://
> Well it turns out my problem was with queues not with threads.  I had
> a self.die prop in my thread object that defaults to FALSE and that I
> set to true when i wanted the thread to die.  then my loop would be
> while not die:  It seemed pretty simple so I didn't know why it was
> failing.  What I didn't know, because I'm quite new to python, is that
> queue.get was blocking.  So my producer thread why dying immediately
> but my worker threads were all blocking on their queue.gets.  So they
> were never falling off the loop.  I changed it to queue.get_nowait()
> and added a queue.empty exception and everything worked as expected.
> So I thought I knew what was going on and that I was having a really
> esoteric problem when i was actually having a pretty boring problem I
> didn't recognize.
> Thanks everybody for the help!>

I've gone through that also, when I started with python threads :-)
Be aware that using get_nowait may lead to your thread using too much
CPU in checking a queue often empty. I tend to use  Queue.get with a
timeout, smaller enough to keep the thread responsive but large enough
to waste CPU in too-frequent checks.


More information about the Python-list mailing list