def start(self): MAX_TIMEOUT = 10 self._running = True # Record which thread this loop is running on. The Server Socket can check this # to decide what to do. self._running_thread = threading.current_thread() _poll_time = 1 _poll = self._poller.poll socket = self._socket if self.profiler is not None: self.profiler.enable() while self._running: socks = dict(_poll()) if len(socks) == 0: _poll_time = min(_poll_time * 2, MAX_TIMEOUT) else: _poll_time = 1 for fd, event in socks.iteritems(): if fd == self._pipe[0]: os.read(fd, 10000) continue if event & zmq.POLLIN: socket.handle_read() if event & zmq.POLLOUT: socket.handle_write() self._poller.register(socket.zmq(), self._direction) # Close serversocket after the loop ends. self._socket.close()
def __init__(self, socket): self.profiler = None self._poller = zmq.Poller() self._running = False self._running_thread = None self._socket = socket self._direction = zmq.POLLIN self._pipe = os.pipe() fcntl.fcntl(self._pipe[0], fcntl.F_SETFL, os.O_NONBLOCK) fcntl.fcntl(self._pipe[1], fcntl.F_SETFL, os.O_NONBLOCK) self._poller.register(self._pipe[0], zmq.POLLIN) self._poller.register(socket.zmq(), self._direction)
def remove(self, socket): with self._lock: assert socket.zmq() in self._sockets self._to_del.append(socket) self.wakeup()
def add(self, socket, direction): util.log_info('Add %s', socket.zmq()) with self._lock: self._to_add.append((socket, direction)) self.wakeup()