Python threads ( termination )

Joshua Marshall jmarshal at mathworks.com
Fri Feb 16 18:52:07 CET 2001


Dan Parisien <dan at eevolved.com> wrote:

> You could use 2 threading.Event instances to communicate between threads 
> and have the 'bad' thread terminate itself.

> There is no real way to terminate a thread from another thread...

> Dan

You can do this if it's the main thread you're trying to kill.  If you
send the process a signal, it's caught by the main thread.  In a
project I'm working on, we need to run arbitrary Python code which may
do Bad Things (like go into an infinite loop).  So we have the main
thread execute this untrusted code, and have a second thread
occasionally checking up on the main one to be sure progress is being
made.  If the main thread is spending too much time executing a chunk
of code, the second thread signals the process, raising an exception
in the main thread, which then moves on and does something productive.

Of course this is only useful if there's only one thread you're
interested in manipulating.



More information about the Python-list mailing list