How to kill a thread?
Diez B. Roggisch
deets at nospam.web.de
Fri Jun 6 12:21:17 EDT 2008
Laszlo Nagy schrieb:
>
>>
>> def run(self):
>> while True:
>> if exit_event.isSet():
>> # Thread exiting
>> return
>> try:
>> data = q_in.get(timeout = .5)
>> except Queue.Empty:
>> continue
>> # ... process data
>>
>> And then in the MainThread I do exit_event.set() and wait for all
>> threads to exit. It's a pretty awkward solution but works.
>>
>> BTW Guys, is something like Thread.kill() planned for the future? Or
>> is there a reason not to have it?
> Python threads are cooperative. I think there was a discussion about
> this, about a year ago or so. The answer was that in CPython, the
> interpreter has this GIL. Python threads are always running on the same
> processor, but they are not "native". There are operating system
> functions to kill a thread but you must not use them because it can
> crash the interpreter.
This is not correct. Neither are threads cooperative nor are they not
native. If they weren't cooperative they needed explicit re-scheduling
statements in the code. And they also are based on native OS threads.
They might *appear* not cooperative if a long-running C-call doesn't
release the GIL beforehand.
This of course doesn't affect other parts of your reasoning about
killing threads being a bad idea and such.
Diez
More information about the Python-list
mailing list