# import os, subprocess, time, threading, signal # timeout = 30 # def timed_out(): # debug("operation timed out!") # os.kill(proc.pid, signal.SIGTERM) # alarm = AlarmTimer(timeout, timed_out) # alarm.start() class AlarmTimer(threading.Thread): def __init__(self, timeout, action): threading.Thread.__init__(self) self.setDaemon(True) self.timeout = timeout self.action = action self.cancelled = False def run(self): # debug("AlarmTimer start") time.sleep(self.timeout) # debug("AlarmTimer sleep done") if not self.cancelled: # debug("AlarmTimer action!") self.action() def cancel(self): # debug("AlarmTimer cancel") self.cancelled = True