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 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 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)