cheroot.connections module

Utilities to manage open connections.

class cheroot.connections.ConnectionManager(server)

Bases: object

Class which manages HTTPConnection objects.

This is for connections which are being kept-alive for follow-up requests.


Expire least recently used connections.

This happens if there are either too many open connections, or if the connections have been timed out.

This should be called periodically.

property _num_connections

Return the current number of connections.

Includes all connections registered with the selector, minus one for the server socket, which is always registered with the selector.


Clean up the resources of any broken connections.

This method attempts to detect any connections in an invalid state, unregisters them from the selector and closes the file descriptors of the corresponding network sockets where possible.

property can_add_keepalive_connection

Flag whether it is allowed to add a new keep-alive connection.


Close all monitored connections.


Put idle connection into the ConnectionManager to be managed.


conn (cheroot.server.HTTPConnection) – HTTP connection to be managed


Run the connections selector indefinitely.

expiration_interval (float): Interval, in seconds, at which

connections will be checked for expiration.

Connections that are ready to process are submitted via self.server.process_conn()

Connections submitted for processing must be put() back if they should be examined again for another request.

Can be shut down by calling stop().


Stop the selector loop in run() synchronously.

May take up to half a second.

class cheroot.connections._ThreadsafeSelector

Bases: object

Thread-safe wrapper around a DefaultSelector.

There are 2 thread contexts in which it may be accessed:
  • the selector thread

  • one of the worker threads in workers/

The expected read/write patterns are:

Notably, this means _ThreadsafeSelector never needs to worry that connections will be removed behind its back.

The lock is held when iterating or modifying the selector but is not required when select()ing on it.


Close the selector.

property connections

Retrieve connections registered with the selector.

register(fileobj, events, data=None)

Register fileobj with the selector.


Return socket fd and data pairs from call.

Returns entries ready to read in the form:

(socket_file_descriptor, connection)


Unregister fileobj from the selector.


Mark the given socket fd as non-inheritable (POSIX).