06Jan Killing a thread in Python
I have been searching for quite a while for information on how to kill a thread in Python and I finally found a great solution. I was just about to implement my own way of doing it but it is not as clean as this way (although killing threads is not clean in the first place).
My thought was to use threading.settrace() and set my own trace function for every thread that is started. That way every thread will have to pass through the trace function in oder to continue executing and it doesn’t matter where in the code it is at or going. In my trace function I was going to add an Event that it would wait on if I want to pause execution of the thread, or check something to make it through an execption to exit out of the thread.
I was in the process of searching for a way to find out if a thread died because of an Exception when I ran across a post here: http://www.dlevel.com/blogs/alex/20
What they have done is added a terminate() method to the Thread class in the threading library. This will throw a SystemExit exception and quitely terminate the thread. Just take the code and paste it into a new module and use that Thread class as opposed to the threading.Thread class.
I agree with what he says about using this. Obviously it is not very safe to do this but in some cases you really need a way to terminate a thread and if you are careful it can work out ok.

