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 sh
I realize this thread has been inactive for some time, but I faced a similar issue and hope that someone else may find this useful as well.
As @jfs mentioned in a useful comment, the standard threading module provides a Timer method that works really well for this (docs). It is just a subclass of threading.Thread, but it makes this very simple and clean. It can also be canceled using the inherited cancel method.
import threading
delay_time = 3 # delay time in seconds
def watchdog():
print('Watchdog expired. Exiting...')
os._exit(1)
alarm = threading.Timer(delay_time, watchdog)
alarm.start()
my_potentially_never_ending_call()
alarm.cancel()