killing thread ?

"Martin v. Löwis" martin at v.loewis.de
Fri Jan 24 10:41:53 EST 2003


Paul Rubin wrote:
> Is there some inherent reason for that?  Or is it just a deficiency,
> either in Python or in the way OS threads work?  I don't understand
> why Python's thread switching code can't just look for a flag
> indicating that the thread should be killed instead of allowed to run.

Python does not implement a thread switching code; these are operating 
system threads.

If you are thinking that the interpreter loop could look whether it 
shall terminate: this works only if the thread is not in a blocking 
system call.

There are thread cancellation functions in some of the underlying thread 
APIs. The main reason why those are not used is that they don't 
implement proper stack unwinding. In Python, you have to explicitly 
complete all C stack frames, because they may need to DECREF objects (in 
particular, argument tuples). So a plain thread termination, as Irmen 
suggests it, leaves garbage behind.

If these issues (blocking system calls and proper unwinding) are 
addressed, the feature could be provided. Most likely, it would be 
exposed as an exception that appears out of nothing in the target 
thread, so if the target thread choses to catch the exception, you still 
couldn't terminate it.

Regards,
Martin





More information about the Python-list mailing list