Killing threads (was Re: Cancel or timeout a long running regular expression)

Chris Angelico rosuav at
Sat Sep 17 23:35:01 CEST 2011

On Sun, Sep 18, 2011 at 5:00 AM, Nobody <nobody at> wrote:
> The only robust solution is to use a separate process (threads won't
> suffice, as they don't have a .kill() method).

Forking a thread to discuss threads.... ahem.

Why is it that threads can't be killed? Do Python threads correspond
to OS-provided threads (eg POSIX threads on Linux)? Every OS threading
library I've seen has some way of killing threads, although I've not
looked in detail into POSIX threads there (there seem to be two
options, pthread_kill and pthread_cancel, that could be used, but I've
not used either). If nothing else, it ought to be possible to
implement a high level kill simply by setting a flag that the
interpreter will inspect every few commands, the same way that
KeyboardInterrupt is checked for.

Is it just that nobody's implemented it, or is there a good reason for
avoiding offering this sort of thing?

Chris Angelico

More information about the Python-list mailing list