Skip to main content

TaskPool

This class provides an eventlet-based task pool for managing concurrent green threads. It leverages GreenPool to execute tasks asynchronously, supporting dynamic resizing of the pool limit and job termination via greenlet killing. The implementation integrates with system signals to track pool lifecycle events and job execution status.

Attributes

AttributeTypeDescription
Timerclass = TimerReference to the Timer class used for scheduling internal events.
signal_safeboolean = FalseBoolean flag indicating if the pool can safely handle POSIX signals, set to False for eventlet.
is_greenboolean = TrueBoolean flag indicating that this pool implementation uses green threads.
task_join_will_blockboolean = FalseBoolean flag indicating whether joining a task will block the entire process.

Constructor

Signature

def TaskPool(
*args: tuple,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
*argstupleVariable length argument list passed to the base class constructor.
**kwargsdictArbitrary keyword arguments passed to the base class constructor.

Methods


on_start()

@classmethod
def on_start()

Initializes the Eventlet GreenPool and internal tracking maps, then broadcasts a pool started signal.


on_stop()

@classmethod
def on_stop()

Gracefully shuts down the pool by waiting for all active greenlets to complete and sending pre/post shutdown signals.


on_apply()

@classmethod
def on_apply(
target: callable,
args: list = None,
kwargs: dict = None,
callback: callable = None,
accept_callback: callable = None
)

Schedules a task for execution within the green pool and registers it in the internal process map for tracking.

Parameters

NameTypeDescription
targetcallableThe function or task to be executed asynchronously
argslist = NonePositional arguments to pass to the target function
kwargsdict = NoneKeyword arguments to pass to the target function
callbackcallable = NoneFunction to be called with the result upon successful task completion
accept_callbackcallable = NoneFunction to be called when the task is officially accepted by the pool

grow()

@classmethod
def grow(
n: int = 1
)

Increases the maximum number of concurrent greenlets allowed in the pool.

Parameters

NameTypeDescription
nint = 1The number of slots to add to the current pool limit

shrink()

@classmethod
def shrink(
n: int = 1
)

Decreases the maximum number of concurrent greenlets allowed in the pool.

Parameters

NameTypeDescription
nint = 1The number of slots to remove from the current pool limit

terminate_job()

@classmethod
def terminate_job(
pid: int,
signal: int = None
)

Forcefully kills a specific greenlet identified by its process ID and waits for it to exit.

Parameters

NameTypeDescription
pidintThe unique identifier of the greenlet to be terminated
signalint = NoneThe signal to send; note that Eventlet greenlets primarily respond to kill() rather than system signals