def serve(self, timeout=1, wait_for_lock=True): # serving """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 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 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: """possible sync request deadlock on at least one windows machine after starting poll thread, force a smaller timeout""" timeout = 0.01 if sys.platform == 'win32' else timeout 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 _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')
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)
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)