class TimeoutGuard(Guard): """ Timeout spawns a timer thread, when posted. If removed before timeout, then the timer thread is cancelled. """ def __init__(self, seconds, action=None): self.seconds = seconds self.posted = (None, None) self.s = Scheduler() self.p = None self.g = (self, action) # Timer expired, offer an active Channel Request def expire(self): op, req = self.posted if op == READ: ChannelReq(self.p, msg=None).offer(req) elif op == WRITE: req.offer(ChannelReq(self.p)) def _post_read(self, reader): self.posted = (READ, reader) # Start process self.p = Process(self.expire) self.p.start() self.p.setstate(ACTIVE) # Put process on the scheduler timer queue self.s.timer_wait(self.p, self.seconds) def _post_write(self, writer): self.posted = (WRITE, writer) # Start process self.p = Process(self.expire) self.p.start() self.p.setstate(ACTIVE) # Put process on the scheduler timer queue self.s.timer_wait(self.p, self.seconds) def _remove_read(self, req): self.s.timer_cancel(self.p) def _remove_write(self, req): self.s.timer_cancel(self.p)
def process(self): p = Process(self.empty) p.start() p.setstate(ACTIVE) return p