Is it possible to kill a thread ?

Peter Hansen peter at engcorp.com
Thu Dec 6 20:49:55 EST 2001


Grant Edwards wrote:
> 
> In article <3C0FCD72.A5FA167C at ccvcorp.com>, Jeff Shannon wrote:
> > Create a threading.Event object, and pass it to the thread at startup.  When
> > you want to close the application, set the event, and then join() the thread.
> > In the thread, you periodically check the event.  When the event becomes set,
> > then the thread terminates itself.  At that point, your main thread returns
> > from join() and you can finish shutting down.
> 
> The problem with this is that one often creates a thread so
> that it can do some possibly blocking operation without
> stopping the rest of the program.  If the thread is blocked, it
> can't check the event.  If the thread isn't going to block,
> then why create a thread?  [Yes, there are other reasons for
> splitting something into a thread, but in many cases I create a
> thread specifically because it's goign to block.]

Have the thread block with a timeout.  Periodically wake up
and check the signal that asks it to die...  (In Python this
is the only clean and practical way to make this work, AFAIK.)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list