This class (hopefully) generalizes the functionality of a pool of threads to which work can be dispatched.

callInThread and stop should only be called from a single thread.

Instance Variable started Whether or not the thread pool is currently running. (type: bool)
Instance Variable threads List of workers currently running in this thread pool. (type: list)
Method __init__ Create a new threadpool.
Method workers For legacy compatibility purposes, return a total number of workers.
Method working For legacy compatibility purposes, return the number of busy workers as expressed by a list the length of that number.
Method waiters For legacy compatibility purposes, return the number of idle workers as expressed by a list the length of that number.
Method start Start the threadpool.
Method startAWorker Increase the number of available workers for the thread pool by 1, up to the maximum allowed by ThreadPool.max.
Method stopAWorker Decrease the number of available workers by 1, by quitting one as soon as it's idle.
Method __setstate__ Undocumented
Method __getstate__ Undocumented
Method callInThread Call a callable object in a separate thread.
Method callInThreadWithCallback Call a callable object in a separate thread and call onResult with the return value, or a twisted.python.failure.Failure if the callable raises an exception.
Method stop Shutdown the threads in the threadpool.
Method adjustPoolsize Adjust the number of available threads by setting min and max to new values.
Method dumpStats Dump some plain-text informational messages to the log about the state of this ThreadPool.
Instance Variable _pool A hook for testing. (type: callable compatible with _pool)
Method _queue For legacy compatibility purposes, return an object with a qsize method that indicates the amount of work not yet allocated to a worker.
Method _generateName Generate a name for a new pool thread.
started =
Whether or not the thread pool is currently running. (type: bool)
threads =
List of workers currently running in this thread pool. (type: list)
_pool =
A hook for testing. (type: callable compatible with _pool)
def __init__(self, minthreads=5, maxthreads=20, name=None): (source)

Create a new threadpool.

Parametersminthreadsminimum number of threads in the pool (type: int)
maxthreadsmaximum number of threads in the pool (type: int)
nameThe name to give this threadpool; visible in log messages. (type: native str)
@property
def workers(self): (source)

For legacy compatibility purposes, return a total number of workers.

Returnsthe current number of workers, both idle and busy (but not those that have been quit by ThreadPool.adjustPoolsize) (type: int)
@property
def working(self): (source)

For legacy compatibility purposes, return the number of busy workers as expressed by a list the length of that number.

Returnsthe number of workers currently processing a work item. (type: list of None)
@property
def waiters(self): (source)

For legacy compatibility purposes, return the number of idle workers as expressed by a list the length of that number.

Returnsthe number of workers currently alive (with an allocated thread) but waiting for new work. (type: list of None)
@property
def _queue(self): (source)

For legacy compatibility purposes, return an object with a qsize method that indicates the amount of work not yet allocated to a worker.

Returnsan object with a qsize method.
def start(self): (source)

Start the threadpool.

def startAWorker(self): (source)

Increase the number of available workers for the thread pool by 1, up to the maximum allowed by ThreadPool.max.

def _generateName(self): (source)

Generate a name for a new pool thread.

ReturnsA distinctive name for the thread. (type: native str)
def stopAWorker(self): (source)

Decrease the number of available workers by 1, by quitting one as soon as it's idle.

def __setstate__(self, state): (source)
Undocumented
def __getstate__(self): (source)
Undocumented
def callInThread(self, func, *args, **kw): (source)

Call a callable object in a separate thread.

Parametersfunccallable object to be called in separate thread
argspositional arguments to be passed to func
kwkeyword args to be passed to func
def callInThreadWithCallback(self, onResult, func, *args, **kw): (source)

Call a callable object in a separate thread and call onResult with the return value, or a twisted.python.failure.Failure if the callable raises an exception.

The callable is allowed to block, but the onResult function must not block and should perform as little work as possible.

A typical action for onResult for a threadpool used with a Twisted reactor would be to schedule a twisted.internet.defer.Deferred to fire in the main reactor thread using .callFromThread. Note that onResult is called inside the separate thread, not inside the reactor thread.

ParametersonResulta callable with the signature (success, result). If the callable returns normally, onResult is called with (True, result) where result is the return value of the callable. If the callable throws an exception, onResult is called with (False, failure).

Optionally, onResult may be None, in which case it is not called at all.

funccallable object to be called in separate thread
argspositional arguments to be passed to func
kwkeyword arguments to be passed to func
def stop(self): (source)

Shutdown the threads in the threadpool.

def adjustPoolsize(self, minthreads=None, maxthreads=None): (source)

Adjust the number of available threads by setting min and max to new values.

ParametersminthreadsThe new value for ThreadPool.min.
maxthreadsThe new value for ThreadPool.max.
def dumpStats(self): (source)

Dump some plain-text informational messages to the log about the state of this ThreadPool.

API Documentation for Twisted, generated by pydoctor at 2018-07-14 04:53:34.