[Python-Dev] Killing threads

Tim Peters tim.one@home.com
Wed, 23 May 2001 15:45:06 -0400


[Aahz]
> Okay, so we all know it isn't possible to kill threads cleanly and
> safely in any kind of cross-platform way.  At the same time, a program
> that has a thread running haywire should be able to kill itself
> completely, so that a monitoring process can restart it.  How hard would
> it be to do only that in a cross-platform way?

Since Python is written in C, and C says nothing about this, you need a
platform expert for each platform covered by "cross" <wink>.

> I'm guessing that for Unix, we'd just send a hard signal (9 or 15).  No
> clue what would need to happen for Windows and Mac.
>
> (This got brought up because I experimented with os._exit() as a
> possible solution, but that GPFs on Win98SE.)

Please open a bug report on that, then, with a tiny test case if possible.
This worked fine on Win98SE for me just now:

import thread, os, time

def task():
    while 1:
        print "x",
        time.sleep(.1)

for i in range(10):
    thread.start_new_thread(task, ())

time.sleep(5)
os._exit(1)

Windows kills all threads spawned by a process when "the main thread" exits.
You don't need to do os._exit(), and sys.exit() is normally a much better
idea (else, e.g., stdio buffers may not get flushed to disk).