reactor: support for delayed callbacks

Signed-off-by: Janar Sööt <janar.soot@gmail.com>
This commit is contained in:
Janar Sööt 2018-12-20 20:56:41 +02:00 committed by Kevin O'Connor
parent e26d1a3567
commit 265769787f
1 changed files with 5 additions and 5 deletions

View File

@ -13,9 +13,9 @@ class ReactorTimer:
self.waketime = waketime self.waketime = waketime
class ReactorCallback: class ReactorCallback:
def __init__(self, reactor, callback): def __init__(self, reactor, callback, waketime):
self.reactor = reactor self.reactor = reactor
self.timer = reactor.register_timer(self.invoke, reactor.NOW) self.timer = reactor.register_timer(self.invoke, waketime)
self.callback = callback self.callback = callback
def invoke(self, eventtime): def invoke(self, eventtime):
self.reactor.unregister_timer(self.timer) self.reactor.unregister_timer(self.timer)
@ -88,8 +88,8 @@ class SelectReactor:
return 0. return 0.
return min(1., max(.001, self._next_timer - self.monotonic())) return min(1., max(.001, self._next_timer - self.monotonic()))
# Callbacks # Callbacks
def register_callback(self, callback): def register_callback(self, callback, waketime = NOW):
ReactorCallback(self, callback) ReactorCallback(self, callback, waketime)
def register_async_callback(self, callback): def register_async_callback(self, callback):
self._async_queue.put_nowait(callback) self._async_queue.put_nowait(callback)
try: try:
@ -106,7 +106,7 @@ class SelectReactor:
callback = self._async_queue.get_nowait() callback = self._async_queue.get_nowait()
except Queue.Empty: except Queue.Empty:
break break
ReactorCallback(self, callback) ReactorCallback(self, callback, self.NOW)
def _setup_async_callbacks(self): def _setup_async_callbacks(self):
self._pipe_fds = os.pipe() self._pipe_fds = os.pipe()
util.set_nonblock(self._pipe_fds[0]) util.set_nonblock(self._pipe_fds[0])