Exemplo n.º 1
0
 def __init__(self,
              port,
              payload_handler,
              io_loop=ioloop.IOLoop.instance(),
              max_connection_num=1024,
              ip_addr="localhost",
              worker_num=2 * cpu_count()):
     # prepares socket
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
     sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     sock.setblocking(False)
     sock.bind((ip_addr, port))
     self._listen_sock = sock
     self._max_connection_num = max_connection_num
     self._net_channels = {}
     # prepares IO loop
     self._io_loop = io_loop
     # prepares process pool
     self._ipc_channels = {}
     self._worker_processes = {}
     self.__next_worker_queue = deque()
     for worker_id in xrange(worker_num):
         server_connection, worker_connection = socket.socketpair()
         ipc_channel = IpcChannel(
             server_connection, worker_id, self._outbound_callback, None,
             functools.partial(self.destory_worker, worker_id),
             self._io_loop)
         self._ipc_channels[worker_id] = ipc_channel
         process = SocketWorker(worker_connection, worker_id,
                                payload_handler)
         self._worker_processes[worker_id] = process
         self.__next_worker_queue.append(worker_id)
Exemplo n.º 2
0
 def __init__(self, connection, worker_id, payload_handler):
     Process.__init__(self)
     self._io_loop = ioloop.IOLoop()
     self._payload_handler = payload_handler
     self._ipc_channel = IpcChannel(connection, worker_id,
                                    self._inbound_callback, None, self.stop,
                                    self._io_loop)
     self._worker_id = worker_id