How to kill a thread?

Nick Craig-Wood nick at craig-wood.com
Sat Jun 7 06:30:21 EDT 2008


David <wizzardx at gmail.com> wrote:
>  Would it be possible for a 3rd-party threading library to an
>  'interruptible' version of Queue?
> 
>  The same methods as the normal Queue, but when you call a new .kill()
>  method on the queue, all the threads locking on the queue get a
>  "QueueKilled" exception thrown.
> 
>  It might be as simple as a Queue wrapper where .kill() adds a speciall
>  Killed value to the queue, and the .get() wrapper throws a QueueKilled
>  exception (after re-putting Killed) when it reads Killed.

There seem to me to be 2 basic types of threads - I'll call them
client threads and server threads.

A client thread listens on a Queue for its instructions and replies
via a different Queue, or via Queues passed in in its instructions.
That kind of thread can easily have an instruction which means quit.
When it isn't processing instructions there is a nice clean moment for
it to quit.  However if the processing takes a long time then there
will be a long wait for the thread to die.  This is the scenario
described above.

The server type thread is processing all the time.  Every now and
again it sends its results via a Queue.  An example of this type of
thread might be one listening on a socket.  This type of thread has no
nice moment to listen for instructions to quit as it might well be
blocked on something else (eg listening to a socket).  To make this
type of thread kill nicely you have to go back to breaking up your
work into chunks (eg polling the socket asynchronously) and polling
the command queue to wait for your quit instruction which is wastefull
of CPU resources.

So it seems to me that there isn't a good solution to killing python
threads of either type, other than coding them carefully and checking
for quit instructions regularly.

You can kill them using the internal python API and ctypes, but you
run the risk of leaving things in an inconsistent state which is why
that API isn't exposed.

Here is an attempt at a killable thread

  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960

and

  http://sebulba.wikispaces.com/recipe+thread2

Read the caveats!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list