def serve(self, timeout=1, wait_for_lock=True): """Serves a single request or reply that arrives within the given time frame (default is 1 sec). Note that the dispatching of a request might trigger multiple (nested) requests, thus this function may be reentrant. :returns: ``True`` if a request or reply were received, ``False`` otherwise. """ timeout = Timeout(timeout) with self._recv_event: if not self._recvlock.acquire(False): return (wait_for_lock and self._recv_event.wait(timeout.timeleft())) try: data = self._channel.poll(timeout) and self._channel.recv() if not data: return False except EOFError: self.close() raise finally: self._recvlock.release() with self._recv_event: self._recv_event.notify_all() self._dispatch(data) return True
def poll(self, timeout): """indicates whether the stream has data to read (within *timeout* seconds)""" timeout = Timeout(timeout) try: p = poll( ) # from lib.compat, it may be a select object on non-Unix platforms p.register(self.fileno(), "r") while True: try: rl = p.poll(timeout.timeleft()) except select_error: ex = sys.exc_info()[1] if ex.args[0] == errno.EINTR: continue else: raise else: break except ValueError: # if the underlying call is a select(), then the following errors may happen: # - "ValueError: filedescriptor cannot be a negative integer (-1)" # - "ValueError: filedescriptor out of range in select()" # let's translate them to select.error ex = sys.exc_info()[1] raise select_error(str(ex)) return bool(rl)
def set_expiry(self, timeout): """Sets the expiry time (in seconds, relative to now) or ``None`` for unlimited time :param timeout: the expiry time in seconds or ``None`` """ self._ttl = Timeout(timeout)
def __init__(self, conn): self._conn = conn self._is_ready = False self._is_exc = None self._obj = None self._callbacks = [] self._ttl = Timeout(None)
def poll(self, timeout, interval=0.1): """a poor man's version of select()""" timeout = Timeout(timeout) try: while True: if win32pipe.PeekNamedPipe(self.incoming, 0)[1] != 0: return True if timeout.expired(): return False timeout.sleep(interval) except TypeError: ex = sys.exc_info()[1] if not self.closed: raise raise EOFError(ex)
def poll_all(self, timeout=0): """Serves all requests and replies that arrive within the given interval. :returns: ``True`` if at least a single transaction was served, ``False`` otherwise """ at_least_once = False timeout = Timeout(timeout) try: while True: if self.poll(timeout): at_least_once = True if timeout.expired(): break except EOFError: pass return at_least_once
def poll(self, timeout, interval=0.001): """Windows version of select()""" timeout = Timeout(timeout) try: if timeout.finite: wait_time = int(max(1, timeout.timeleft() * 1000)) else: wait_time = win32event.INFINITE if not self.poll_read: hr, self.poll_buffer = win32file.ReadFile(self.incoming, self.poll_buffer, self.read_overlapped) self.poll_read = True if hr == 0: return True res = win32event.WaitForSingleObject(self.read_overlapped.hEvent, wait_time) return res == win32event.WAIT_OBJECT_0 except TypeError: ex = sys.exc_info()[1] if not self.closed: raise raise EOFError(ex)
class AsyncResult(object): """*AsyncResult* represents a computation that occurs in the background and will eventually have a result. Use the :attr:`value` property to access the result (which will block if the result has not yet arrived). """ __slots__ = ["_conn", "_is_ready", "_is_exc", "_callbacks", "_obj", "_ttl"] def __init__(self, conn): self._conn = conn self._is_ready = False self._is_exc = None self._obj = None self._callbacks = [] self._ttl = Timeout(None) def __repr__(self): if self._is_ready: state = "ready" elif self._is_exc: state = "error" elif self.expired: state = "expired" else: state = "pending" return "<AsyncResult object (%s) at 0x%08x>" % (state, id(self)) def __call__(self, is_exc, obj): if self.expired: return self._is_exc = is_exc self._obj = obj self._is_ready = True for cb in self._callbacks: cb(self) del self._callbacks[:] def wait(self): """Waits for the result to arrive. If the AsyncResult object has an expiry set, and the result did not arrive within that timeout, an :class:`AsyncResultTimeout` exception is raised""" while not self._is_ready and not self._ttl.expired(): self._conn.serve(self._ttl) if not self._is_ready: raise AsyncResultTimeout("result expired") def add_callback(self, func): """Adds a callback to be invoked when the result arrives. The callback function takes a single argument, which is the current AsyncResult (``self``). If the result has already arrived, the function is invoked immediately. :param func: the callback function to add """ if self._is_ready: func(self) else: self._callbacks.append(func) def set_expiry(self, timeout): """Sets the expiry time (in seconds, relative to now) or ``None`` for unlimited time :param timeout: the expiry time in seconds or ``None`` """ self._ttl = Timeout(timeout) @property def ready(self): """Indicates whether the result has arrived""" if self._is_ready: return True if self._ttl.expired(): return False self._conn.poll_all() return self._is_ready @property def error(self): """Indicates whether the returned result is an exception""" return self.ready and self._is_exc @property def expired(self): """Indicates whether the AsyncResult has expired""" return not self._is_ready and self._ttl.expired() @property def value(self): """Returns the result of the operation. If the result has not yet arrived, accessing this property will wait for it. If the result does not arrive before the expiry time elapses, :class:`AsyncResultTimeout` is raised. If the returned result is an exception, it will be raised here. Otherwise, the result is returned directly. """ self.wait() if self._is_exc: raise self._obj else: return self._obj