killing thread ?

Laura Creighton lac at strakt.com
Fri Jan 24 10:18:45 EST 2003


> Laura Creighton <lac at strakt.com> wrote:
> >> 
> >> Hi,
> >> 
> >> AFAIK there is no method in the threading module to kill a thread.
> >> Each thread has to terminate itself by watching status variables.
> >> But: what happens if I start a thread, and when I want to kill
> >> this thread, I delete all references to the according Thread object,
> >> and call the garbage collector. 
> >> Will this work ??
> 
> > No.  You cannot stand outside your application and kill a thread.
> > Instead you have to arrange for your thread to receive the message
> > to go commit suicide (and then your thread has to know how to do
> > that).  If your threads are already communicating by Queues, sticking
> > a 'do_die' in there is often cleanest.  
> 
> > <snip> 
> 
> But what if I want to end a thread which hangs because
> it uses 'exec' on some code which loops forever ?

You lose.  That thread doesn't know how to receive a message that it
ought to die.

> 
> I discovered the _Thread__stop() method of threading.Thread object.
> It works. But that seems to be some dirty hack...

I don't think that this does what you think it does.  If you call it in 
the context of another thread, all sorts of weird things happen, but
none of them is what you want.  What you want is a way to stand outside
of your program and shoot the running process that you don't need any
more, and _you_ _can't_ _have_ _that_, because there is no way to do
that, Sorry.

> Dr. rer. nat. Uwe Schmitt      Computer science is no more about Computers,
> uwe.schmitt at num.uni-sb.de      than astronomy is about telescopes. (Dijkstra)
> http://www.procoders.net           

Laura 





More information about the Python-list mailing list