gevent.baseserver – Base class for implementing servers

class gevent.baseserver.BaseServer(listener, handle=None, spawn='default')

An abstract base class that implements some common functionality for the servers in gevent.

listener can either be an address that the server should bind on or a gevent.socket.socket instance that is already bound (and put into listening mode in case of TCP socket).

spawn, if provided, is called to create a new greenlet to run the handler. By default, gevent.spawn() is used.

Possible values for spawn:

  • a gevent.pool.Pool instance – handle will be executed using Pool.spawn() method only if the pool is not full. While it is full, all the connection are dropped;
  • gevent.spawn_raw()handle will be executed in a raw greenlet which have a little less overhead then gevent.Greenlet instances spawned by default;
  • Nonehandle will be executed right away, in the Hub greenlet. handle cannot use any blocking functions as it means switching to the Hub.
  • an integer – a shortcut for gevent.pool.Pool(integer)
closed
fatal_errors = (9, 22, 88)
max_accept = 100
max_delay = 1
min_delay = 0.01
server_host

IP address that the server is bound to (string).

server_port

Port that the server is bound to (an integer).

started
stop_timeout = 1
set_listener(listener)
set_spawn(spawn)
set_handle(handle)
start_accepting()
stop_accepting()
do_handle(*args)
full()
init_socket()

If the user initialized the server with an address rather than socket, then this function will create a socket, bind it and put it into listening mode.

It is not supposed to be called by the user, it is called by start() before starting the accept loop.

start()

Start accepting the connections.

If an address was provided in the constructor, then also create a socket, bind it and put it into the listening mode.

close()

Close the listener socket and stop accepting.

stop(timeout=None)

Stop accepting the connections and close the listening socket.

If the server uses a pool to spawn the requests, then stop() also waits for all the handlers to exit. If there are still handlers executing after timeout has expired (default 1 second), then the currently running handlers in the pool are killed.

serve_forever(stop_timeout=None)

Start the server if it hasn’t been already started and wait until it’s stopped.

is_fatal_error(ex)

Navigation

Related pages