How to kill a thread?

Ian Bicking ianb at colorstudy.com
Wed Jun 11 12:19:12 EDT 2008


On Jun 7, 6:30 am, Nick Craig-Wood <n... at craig-wood.com> wrote:
> Here is an attempt at a killable thread
>
>  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960
>
> and
>
>  http://sebulba.wikispaces.com/recipe+thread2

I use this recipe in paste.httpserver to kill wedged threads, and it
works okay.  It seems to kill threads that are genuinely wedged, e.g.,
if you try to read past the end of a socket.  It takes surprisingly
long, up to a minute to actually send the exception to a thread.  But
it's just a normal exception and seems to act reasonably.

Obviously this is not a great system, but it's better than having a
background process hang because of problem code elsewhere in the
system.  It also isn't any worse than kill -9, which is ultimately
what a multiprocess system has to do to misbehaving worker processes.
Still, to try to mitigate problems with this I set up a supervisor
process and if there is a lot of problems with worker threads I'll
have the entire process self-destruct so it can be restarted by the
supervisor.

The code is rather... organic in its development.  But you can see it
in the ThreadPool here:
http://svn.pythonpaste.org/Paste/trunk/paste/httpserver.py

  Ian



More information about the Python-list mailing list