Killing/Terminating a running thread

Chris Liechti cliechti at
Sat Sep 7 18:05:57 EDT 2002

Heiko Wundram <heikowu at> wrote in
news:mailman.1031434090.15525.python-list at 

> Am Sam, 2002-09-07 um 22.42 schrieb Chris Liechti:
>> sound like the news thread "Interrupting Python" could be
>> interresting fo 
> r 
>> you...
> I've looked at that, but the main problem is that this thread deals
> with interrupting fullblown processes on the fly (from within the
> code), not with interrupting a process by an exterior signal.

ah, sorry. i dont follow that thread.

>> the clean solution is to test for a flag and terminate if the flag is
>> set 
> .
>> e.g. you could provide a function:
>> def interrupted():
>>          return global_flag_if_user_wants_to_abort
>> and in you function in the queue:
>> def worker():
>>          while not interrupted():
>>                   do_stuff()
> This is what I do to shutdown the worker cleanly. The problem is:
> do_stuff() doesn't return until it's complete, and I have no control
> whatsoever about the internal workings of do_stuff().

then you have a problem... if you could enforce the use of the intterupted 
function or flag it would work flawlessly on all platforms.

> What I actually wish to do is while the worker still is in do_stuff(),
> to kill the worker as a whole, to discard all its data, when a timeout
> on returning a value from do_stuff() occurs. It's not about clean
> termination of a thread, but about killing the thread in case it takes
> too long (in case do_stuff() takes to long).

it's maybe possible to kill threads on linux, but i think its a risky thing 
to do anyway. it could have an impact on stability of the other/main 

you cold start worker _processes_ you can kill those cleanly (maybe not on 
Win9x but on every real OS ;-). that requires inter-process communication. 
there are diffrent sorlutions to achieve that, e.g. "pyro" or a corba 
if you realy need to kill a worker and have no controll over its 
implementation, then i think that you can't avoid using processes.

> Thanks for the advice anyway! :) I guess I at least did the general
> interrupt on the worker right then... :)

Chris <cliechti at>

More information about the Python-list mailing list