class Sequencer(object): def __init__(self, poller=None): self._expireq = [] self._poller = poller or asyncio.Poll() self._duration_timer = FDTimer(CLOCK_MONOTONIC, nonblocking=1) self._poller.register_fd(self._duration_timer.fileno(), asyncio.EPOLLIN, self._duration_timeout) def __del__(self): self.close() def fileno(self): return self._poller.fileno() poller = property(lambda self: self._poller) def close(self): if self._poller is not None: self._poller.unregister_all() self._duration_timer.close() for expire, task in self._expireq: task.close() self._poller = None self._duration_timer = None self._expireq = [] def add_task(self, callback, period=0.0, delay=0.0, duration=None, callback_args=()): task = PeriodicTask(callback, period, delay, callback_args) self._poller.register(task) if duration: expire = monotonic() + duration + delay heappush(self._expireq, (expire, task)) self._duration_timer.settime(self._expireq[0][0], 0.0, absolute=True) def _duration_timeout(self): count = self._duration_timer.read() expire, task = heappop(self._expireq) task.stop() self._poller.unregister(task) task.close() if self._expireq: self._duration_timer.settime(self._expireq[0][0], 0.0, absolute=True) else: self._duration_timer.settime(0.0, 0.0) #self._poller.unregister_fd(self._duration_timer.fileno()) def run(self, progress_cb=None): poller = self._poller while self._expireq: poller.poll(5.0) if progress_cb is not None: progress_cb(len(self._expireq))
def __init__(self, callback, period=0.0, delay=0.0, callback_args=()): self._callback = callback self._args = callback_args self._period = period self._timer = timer = FDTimer() if delay: timer.settime(delay, period) else: timer.settime(period, period)
def __init__(self, poller=None): self._expireq = [] self._poller = poller or asyncio.Poll() self._duration_timer = FDTimer(CLOCK_MONOTONIC, nonblocking=1) self._poller.register_fd(self._duration_timer.fileno(), asyncio.EPOLLIN, self._duration_timeout)