gevent.baseserver – Base class for implementing servers#

Base class for implementing servers

class BaseServer(listener, handle=None, spawn='default')[source]#

Bases: object

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

  • listener – 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).

  • handle

    If given, the request handler. The request handler can be defined in a few ways. Most commonly, subclasses will implement a handle method as an instance method. Alternatively, a function can be passed as the handle argument to the constructor. In either case, the handler can later be changed by calling set_handle().

    When the request handler returns, the socket used for the request will be closed. Therefore, the handler must not return if the socket is still in use (for example, by manually spawned greenlets).

  • spawn

    If provided, is called to create a new greenlet to run the handler. By default, gevent.spawn() is used (meaning there is no artificial limit on the number of concurrent requests). Possible values for spawn:

    • a gevent.pool.Pool instance – handle will be executed using gevent.pool.Pool.spawn() only if the pool is not full. While it is full, no new connections are accepted;

    • gevent.spawn_raw()handle will be executed in a raw greenlet which has 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 would mean switching to the Hub.

    • an integer – a shortcut for gevent.pool.Pool(integer)

Changed in version 1.1a1: When the handle function returns from processing a connection, the client socket will be closed. This resolves the non-deterministic closing of the socket, fixing ResourceWarnings under Python 3 and PyPy.

Changed in version 1.5: Now a context manager that returns itself and calls stop() on exit.


Close the listener socket and stop accepting.


If the user initialized the server with an address rather than socket, then this function must 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 the server if it hasn’t been already started and wait until it’s stopped.


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.


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, stop_timeout), then the currently running handlers in the pool are killed.

If the server does not use a pool, then this merely stops accepting connections; any spawned greenlets that are handling requests continue running until they naturally complete.

max_accept = 100#

Sets the maximum number of consecutive accepts that a process may perform on a single wake up. High values give higher priority to high connection rates, while lower values give higher priority to already established connections. Default is 100.

Note that, in case of multiple working processes on the same listening socket, it should be set to a lower value. (pywsgi.WSGIServer sets it to 1 when environ["wsgi.multiprocess"] is true)

This is equivalent to libuv’s uv_tcp_simultaneous_accepts value. Setting the environment variable UV_TCP_SINGLE_ACCEPT to a true value (usually 1) changes the default to 1 (in libuv only; this does not affect gevent).

min_delay = 0.01#

the number of seconds to sleep in case there was an error in accept() call for consecutive errors the delay will double until it reaches max_delay when accept() finally succeeds the delay will be reset to min_delay again

property server_host#

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

property server_port#

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

stop_timeout = 1#

the default timeout that we wait for the client connections to close in stop()