def __init__(self, max_requests, child_conn, protocol, server_socket=None, manager=None, args=None, kwargs=None): """ Initialize the passed in child info and call the initialize() hook """ # Add handling here for SO_REUSEPORT. server_socket will be None # if we can reuse port if not server_socket: if not hasattr(socket, 'SO_REUSEPORT'): self._error('server socket is None and SO_REUSEPORT is not ' 'available. Cannot start child process') os._exit(1) elif not manager: self._error( 'server socket is not set, and neither is the ' 'manager object. One must be set. Cannot start child ' 'process') os._exit(1) self.pre_bind() server_socket = self._get_server_socket(manager) self.post_bind() self.log('Created child') self._server_socket = server_socket self.socket_already_close = False self._max_requests = max_requests self._child_conn = child_conn self._poll = get_poller(select.POLLIN | select.POLLPRI) self._poll.register(self._server_socket) self._poll.register(self._child_conn) self.protocol = protocol self.requests_handled = 0 # The "conn" will be a socket connection object if this is a tcp # server, and will actually be the payload if this is a udp server self.conn = None self.address = None self.closed = False self.error = None args = args if args else [] kwargs = kwargs if kwargs else {} self.initialize(*args, **kwargs)
def __init__(self , max_requests , child_conn , protocol , server_socket=None , manager=None , args=None , kwargs=None): """ Initialize the passed in child info and call the initialize() hook """ # Add handling here for SO_REUSEPORT. server_socket will be None # if we can reuse port if not server_socket: if not hasattr(socket , 'SO_REUSEPORT'): self._error('server socket is None and SO_REUSEPORT is not ' 'available. Cannot start child process') os._exit(1) elif not manager: self._error('server socket is not set, and neither is the ' 'manager object. One must be set. Cannot start child ' 'process') os._exit(1) self.pre_bind() server_socket = self._get_server_socket(manager) self.post_bind() self.log('Created child') self._server_socket = server_socket self.socket_already_close = False self._max_requests = max_requests self._child_conn = child_conn self._poll = get_poller(select.POLLIN | select.POLLPRI) self._poll.register(self._server_socket) self._poll.register(self._child_conn) self.protocol = protocol self.requests_handled = 0 # The "conn" will be a socket connection object if this is a tcp # server, and will actually be the payload if this is a udp server self.conn = None self.address = None self.closed = False self.error = None args = args if args else [] kwargs = kwargs if kwargs else {} self.initialize(*args, **kwargs)
def __init__(self, child_class, child_args=None, child_kwargs=None, max_servers=20, min_servers=5, min_spare_servers=2, max_spare_servers=10, max_requests=0, bind_ip='127.0.0.1', port=10000, protocol='tcp', listen=5, reuse_port=False): """ child_class<BaseChild> : An implentation of BaseChild to define the child processes child_args<list_type> : The argument list to pass into the child initialize() method child_kwargs<list_type> : The argument dict to pass into the child initialize() method max_servers<int> : Maximum number of children to have min_servers<int> : Minimum number of children to have min_spare_servers<int> : Minimum number of spare children to have max_spare_servers<int> : Maximum number of spare children to have max_requests<int> : Maximum number of requests each child should handle. Zero is unlimited and default bind_ip<str> : The IP address to bind to port<int> : The port that the server should listen on protocol<str> : The protocol to use (tcp or udp) listen<int> : Listen backlog reuse_port<bool> : This will use SO_REUSEPORT and create the listen sock in each child rather than in the parent process. SO_REUSEPORT will result in a much more balanced distribution of connections and it is highly recommended that you turn this on if available """ self.log('init') if not child_args: child_args = [] if not child_kwargs: child_kwargs = {} self._ChildClass = child_class # Check for proper typing of child args if not isinstance(child_args, (tuple, list)): raise TypeError('child_args must be a tuple or list type') if not isinstance(child_kwargs, dict): raise TypeError('child_kwargs must be a dict type') self._child_args = child_args self._child_kwargs = child_kwargs self.max_servers = int(max_servers) self.min_servers = int(min_servers) if self.min_servers > self.max_servers: raise ManagerError('You cannot have minServers ' '(%d) be larger than maxServers (%d)!' % (min_servers, max_servers)) self.min_spares = int(min_spare_servers) self.max_spares = int(max_spare_servers) if self.min_spares > self.max_spares: raise ManagerError('You cannot have minSpareServers be larger ' 'than maxSpareServers!') self.max_requests = int(max_requests) self.bind_ip = bind_ip self.port = int(port) self.protocol = protocol.lower() if protocol not in self.validProtocols: raise ManagerError('Invalid protocol %s, must be in: %r' % (protocol, self.validProtocols)) self.listen = int(listen) self.reuse_port = reuse_port and hasattr(socket, 'SO_REUSEPORT') self.log('reuse port: ' + str(self.reuse_port)) self.server_socket = None self._stop = threading.Event() self._children = {} self._poll = get_poller(select.POLLIN | select.POLLPRI) # Bind the socket now so that it can be used before run is called # Addresses: https://github.com/crustymonkey/py-prefork-server/pull/3 if not self.reuse_port: self.pre_bind() self._bind() self.post_bind()
def __init__(self, child_class, child_args=None, child_kwargs=None, max_servers=20, min_servers=5, min_spare_servers=2, max_spare_servers=10, max_requests=0, bind_ip='127.0.0.1', port=10000, protocol='tcp', listen=5 , reuse_port=False): """ child_class<BaseChild> : An implentation of BaseChild to define the child processes child_args<list_type> : The argument list to pass into the child initialize() method child_kwargs<list_type> : The argument dict to pass into the child initialize() method max_servers<int> : Maximum number of children to have min_servers<int> : Minimum number of children to have min_spare_servers<int> : Minimum number of spare children to have max_spare_servers<int> : Maximum number of spare children to have max_requests<int> : Maximum number of requests each child should handle. Zero is unlimited and default bind_ip<str> : The IP address to bind to port<int> : The port that the server should listen on protocol<str> : The protocol to use (tcp or udp) listen<int> : Listen backlog reuse_port<bool> : This will use SO_REUSEPORT and create the listen sock in each child rather than in the parent process. SO_REUSEPORT will result in a much more balanced distribution of connections and it is highly recommended that you turn this on if available """ if not child_args: child_args = [] if not child_kwargs: child_kwargs = {} self._ChildClass = child_class # Check for proper typing of child args if not isinstance(child_args, (tuple, list)): raise TypeError('child_args must be a tuple or list type') if not isinstance(child_kwargs , dict): raise TypeError('child_kwargs must be a dict type') self._child_args = child_args self._child_kwargs = child_kwargs self.max_servers = int(max_servers) self.min_servers = int(min_servers) if self.min_servers > self.max_servers: raise ManagerError('You cannot have minServers ' '(%d) be larger than maxServers (%d)!' % (min_servers, max_servers)) self.min_spares = int(min_spare_servers) self.max_spares = int(max_spare_servers) if self.min_spares > self.max_spares: raise ManagerError('You cannot have minSpareServers be larger ' 'than maxSpareServers!') self.max_requests = int(max_requests) self.bind_ip = bind_ip self.port = int(port) self.protocol = protocol.lower() if protocol not in self.validProtocols: raise ManagerError('Invalid protocol %s, must be in: %r' % (protocol, self.validProtocols)) self.listen = int(listen) self.reuse_port = reuse_port and hasattr(socket , 'SO_REUSEPORT') self.server_socket = None self._stop = threading.Event() self._children = {} self._poll = get_poller(select.POLLIN | select.POLLPRI) # Bind the socket now so that it can be used before run is called # Addresses: https://github.com/crustymonkey/py-prefork-server/pull/3 if not self.reuse_port: self.pre_bind() self._bind() self.post_bind()