import threading, time 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): time.sleep(self.timeout) if not self.cancelled: self.action() def cancel(self): self.cancelled = True def done(): print "hi timeout" AlarmTimer(1, done).start() time.sleep(10)