Killing a Thread

Gabriel Genellina gagsl-py2 at
Fri Jan 29 22:17:52 CET 2010

En Thu, 28 Jan 2010 10:25:30 -0300, Richard Lamboj  
<richard.lamboj at> escribió:

> which Method is better to kill a Thread? Using Thread Events, or a  
> raising a
> Exception? Maybe someone has a small example for me?

The best way is simply NOT to do that. You don't kill a thread, you ask it  
to commit suicide.  There is no reliable way to forcefully shut down  
another thread.

The thread must periodically check some value (perhaps an Event object; in  
simple cases any variable will do) and (cleanly) exit when asked to.

Threads that run pure Python code may be interrupted using the API call  
PyThreadState_SetAsyncExc; look for a recipe using ctypes to call it from  

Gabriel Genellina

More information about the Python-list mailing list