signal.alarm replacement in Windows [Python]

eduffy picture eduffy · Mar 13, 2009 · Viewed 9.2k times · Source

I have a function that occasionally hangs.

Normally I would set an alarm, but I'm in Windows and it's unavailable.

Is there a simple way around this, or should I just create a thread that calls time.sleep()?

Answer

Rhamphoryncus picture Rhamphoryncus · Mar 30, 2009

The most robust solution is to use a subprocess, then kill that subprocess. Python2.6 adds .kill() to subprocess.Popen().

I don't think your threading approach works as you expect. Deleting your reference to the Thread object won't kill the thread. Instead, you'd need to set an attribute that the thread checks once it wakes up.