def poll(self, timeout, interval=0.001): """a Windows 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(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): # serving """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_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 _serve(self): try: """poll_all() ignores EOFError, use poll() instead higher timeout: lower client latency, less responsive ui lower timeout: higher client latency, more responsive ui""" timeout = Timeout(0.025) while not timeout.expired(): self._connection.poll(timeout.timeleft()) except: try: self._connection.close() except: pass self._connection = None self._disconnection_callback() self._log_message('client disconnected')
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 # if is_py3k and type(obj) is bytes: # obj = obj.decode("utf-8") 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
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