def __init__(self, processor, lsocket, inputProtocolFactory=None, outputProtocolFactory=None, threads=10, readTimeout=None, maxQueueSize=0): self.processor = self._getProcessor(processor) self.socket = lsocket self.in_protocol = inputProtocolFactory or TBinaryProtocolFactory() self.out_protocol = outputProtocolFactory or self.in_protocol self.threads = int(threads) self.clients = {} self.max_queue_size = maxQueueSize # do not set this as a hard size # maximum - the queue may need # extra space for close() self.tasks = Queue.Queue() self._read, self._write = _create_socketpair() self.prepared = False self._stop = False self.serverEventHandler = TServer.TServerEventHandler() self.select_timeout = DEFAULT_SELECT_TIMEOUT self.poller = TSocket.ConnectionEpoll() if hasattr(select, "epoll") \ else TSocket.ConnectionSelect() self.last_logged_error = 0 timeouts = [x for x in [self.select_timeout, readTimeout] \ if x is not None] if len(timeouts) > 0: self.select_timeout = min(timeouts) self._readTimeout = readTimeout
def test_poller_process(self): # Make sure that pollers do not fail when they're given None as timeout text = "hi" # sample text to send over the wire with TSocket.TServerSocket(port=0, family=socket.AF_INET6) as server: addr = server.getSocketNames()[0] def write_data(): # delay writing to verify that poller.process is waiting time.sleep(1) with TSocket.TSocket(host=addr[0], port=addr[1]) as conn: conn.write(text) poller = TSocket.ConnectionSelect() thread = threading.Thread(target=write_data) thread.start() for filenos in server.handles.keys(): poller.read(filenos) r, _, x = poller.process(timeout=None) thread.join() # Verify that r is non-empty self.assertTrue(r)
def _select(self): """Does epoll or select on open connections.""" # We may not have epoll on older systems. if self.use_epoll: poller = TSocket.ConnectionEpoll() else: poller = TSocket.ConnectionSelect() for fileno in self.socket.handles: poller.read(fileno) poller.read(self._read.fileno()) readExpiration = None # If the last read time was older than this, close the connection. if self._readTimeout: readExpiration = time.time() - self._readTimeout if sys.version_info[0] >= 3: item_list = list(self.clients.items()) else: item_list = self.clients.items() for i, connection in item_list: if connection.is_readable(readExpiration): poller.read(connection.fileno()) if connection.is_writeable(): poller.write(connection.fileno()) if connection.is_closed(): connection.connectionDestroyed() del self.clients[i] try: return poller.process(self.select_timeout) except Exception as e: if not (isinstance(e, IOError) and e.errno == errno.EINTR): self.log_poll_problem("problem polling: %s" % e) return [], [], []