Killing/Terminating a running thread
cliechti at gmx.net
Sat Sep 7 16:42:28 EDT 2002
Heiko Wundram <heikowu at ceosg.de> wrote in
news:mailman.1031419029.11871.python-list at python.org:
> Hi to all!
> I've looked over google groups, but I've found no real answer that
> satisfied the needs I have. Just a little explanation:
> I have a Queue module, which stores method-calls and delegates them to
> threads, which take over running the call. Now it happens that a method
> takes much longer to complete than I would like it to, and in this case,
> the thread running the code should just be terminated, discarding all
> values computed so far.
sound like the news thread "Interrupting Python" could be interresting for
> The basic problem I have that it is pretty much impossible to get at the
> PID of the thread running the code in question; but I have a reference
> to the thread class in a central "garbage-collector" (the queue object).
> And anyway, this approach would only work on Unix (I guess), when using
> pthreads, which create a "real" subprocess with its own PID for a
> Has anyone had a similar problem, and know of a solution?
the clean solution is to test for a flag and terminate if the flag is set.
e.g. you could provide a function:
and in you function in the queue:
while not interrupted():
you can do that of course with or without a function. a function has the
benefit that you could do additional checks, such as stopping only a
specific thread/worker. (e.g. threading.currentThread() could be used to
test if it the thread to be killed)
Chris <cliechti at gmx.net>
More information about the Python-list