trying to kill threads

Peter Hansen peter at engcorp.com
Fri Feb 7 19:35:22 EST 2003


anton wilson wrote:
> 
> I'm trying to figure out how to kill a python thread in a way that won't
> trash the interpreter or memory. I have a program that allows a user to use
> our pre-defined python functions to create threads and run his/her own
> function. But when the stop button is pressed, I want to get rid of the
> threads the user created fairly quickly.
> 
> I'm sure every understands the futility of using kill() with python.
> 
> Any suggestions?

There are *many* and *frequent* discussions of this in this newsgroup/
mailing list.  Please check the archives for background, but the short
answer is "you can't do exactly this".

The workaround is to provide for a flag which is periodically checked
by the threads and when the flag is set, they must kill themselves.
There are variations possible using Event objects, and different
approaches perhaps more appropriate to one situation or another, such
as when you've subclassed threading.Thread, but they are all built
around the basic concept of "requested suicide", not "murder".

-Peter




More information about the Python-list mailing list