class Timer(Callback.Callbacks): def __init__(self, factory, duration=0.0): super(Timer, self).__init__() self.tfact = factory self.duration = duration # For storing aritrary data with timers self.data = Bunch.Bunch() self.timer = HeapTimer(self.tfact.timer_heap, 0, self._expired_cb) for name in ('expired', 'canceled'): self.enable_callback(name) def set(self, time_sec): """ Set the timer for time_sec. Any callbacks registered for the 'expired' event will be called when the timer reaches the deadline. """ #self.timer.stop() self.timer.start(time_sec) def start(self, duration=None): if duration is None: duration = self.duration self.set(duration) def is_set(self): """ Returns True if this timer is set. """ return self.timer.is_scheduled() def _expired_cb(self): self.make_callback('expired') def cond_set(self, time_sec): self.timer.cond_start(time_sec) def time_left(self): return self.timer.remaining_time() def get_deadline(self): return self.timer.expiration_time() def stop(self): self.timer.stop() def cancel(self): """ Clear a pending expiration event. """ self.stop() self.make_callback('canceled') clear = cancel
def __init__(self, factory): super(Timer, self).__init__() self.tfact = factory # For storing aritrary data with timers self.data = Bunch.Bunch() self.timer = HeapTimer(self.tfact.timer_heap, 0, self._expired_cb) for name in ('expired', 'canceled'): self.enable_callback(name)