cheroot.workers.threadpool module

A thread-based worker pool.

class cheroot.workers.threadpool.ThreadPool(server, min=10, max=-1, accepted_queue_size=-1, accepted_queue_timeout=10)

Bases: object

A Request Queue for an HTTPServer which pools threads.

ThreadPool objects must provide min, get(), put(obj), start() and stop(timeout) attributes.

_clear_dead_threads()
_decide_retirement(worker, request)

Decide whether a worker that took a retirement request retires.

The retirement stops being due whatever the answer, and the worker is marked as retiring when it is yes.

static _force_close(conn)
_grow(amount)
_serving()

Return the threads that run and that are not retiring.

_spawn_worker()
_stops(worker)

Tell whether a worker that took a stop request shuts down.

_wait_for(threads, timeout, endtime)

Wait for threads to exit, closing connections after the timeout.

grow(amount)

Spawn new worker threads (not above self.max).

Raises:

RuntimeError – while stop() ends the threads

property idle

Number of worker threads which are idle. Read-only.

put(obj)

Put request into queue.

Args:
obj (HTTPConnection): HTTP connection

waiting to be processed

property qsize

The queue size.

shrink(amount)

Kill off worker threads (not below self.min).

Threads that earlier calls asked to retire count as gone.

start()

Start the pool of threads.

Raises:

RuntimeError – if the pool is already started

stop(timeout=5)

Terminate all worker threads.

One call at a time ends the threads. A call from a thread of the pool returns at once while another call does. Any other call waits for that one to finish, then ends the threads still running.

Args:
timeout (int): time to wait for threads to stop gracefully,

counted from the call, before closing their connections

class cheroot.workers.threadpool.WorkerThread(server)

Bases: Thread

Thread which continuously polls a Queue for Connection objects.

Due to the timing issues of polling a Queue, a WorkerThread does not check its own ‘ready’ flag after it has started. To stop the thread, it is necessary to stick a _SHUTDOWNREQUEST object onto the Queue (one for each running WorkerThread).

_get_request()

Get a connection, or a shutdown request for this thread to exit.

_process_connections_until_interrupted()

Process incoming HTTP connections in an infinite loop.

Retrieves incoming connections from thread pool, processing them one by one.

Raises:

SystemExit – on the internal requests to stop the server instance

_retiring = False

Whether this thread took a retirement or a stop request, and exits.

_run = None

The run of the pool that started this thread, if one did.

conn = None

The current connection pulled off the Queue, or None.

ready = False

A simple flag for the calling server to know when this thread has begun polling the Queue.

run()

Set up incoming HTTP connection processing loop.

This is the thread’s entry-point. It performs lop-layer exception handling and interrupt processing. KeyboardInterrupt and SystemExit bubbling up from the inner-layer code constitute a global server interrupt request. When they happen, the worker thread exits.

Raises:

BaseException – when an unexpected non-interrupt exception leaks from the inner layers

# noqa: DAR401 KeyboardInterrupt SystemExit

server = None

The HTTP Server which spawned this thread, and which owns the Queue and is placing active connections into it.