Skip to main content

TaskPool

GEvent Pool.

Attributes

AttributeTypeDescription
Timerclass = TimerReference to the Timer class used for scheduling timed events within the pool.
signal_safeboolean = falseBoolean flag indicating if the pool can safely handle POSIX signals, set to False for gevent compatibility.
is_greenboolean = trueBoolean flag indicating that this pool implementation uses greenlets for concurrency.
task_join_will_blockboolean = falseIndicates whether joining a task will block the current execution thread.

Constructor

Signature

def TaskPool(
*args: any,
**kwargs: any
)

Parameters

NameTypeDescription
*argsanyVariable length argument list passed to the base class constructor.
**kwargsanyArbitrary keyword arguments, including 'timeout' for task execution.

Methods


on_start()

@classmethod
def on_start() - > null

Initializes the GEvent pool and internal tracking structures required for managing greenlets.

Returns

TypeDescription
null

on_stop()

@classmethod
def on_stop() - > null

Gracefully shuts down the pool by waiting for all currently executing greenlets to complete their tasks.

Returns

TypeDescription
null

on_apply()

@classmethod
def on_apply(
target: callable,
args: list = None,
kwargs: dict = None,
callback: callable = None,
accept_callback: callable = None,
timeout: float = None,
timeout_callback: callable = None,
apply_target: callable = apply_target
) - > greenlet.greenlet

Schedules a task for execution within the greenlet pool, supporting timeouts and completion callbacks.

Parameters

NameTypeDescription
targetcallableThe function or task to be executed within the pool.
argslist = NonePositional arguments to pass to the target function.
kwargsdict = NoneKeyword arguments to pass to the target function.
callbackcallable = NoneFunction to be invoked upon successful completion of the task.
accept_callbackcallable = NoneFunction to be invoked when the task is accepted by the pool.
timeoutfloat = NoneMaximum time in seconds allowed for task execution before it is aborted.
timeout_callbackcallable = NoneFunction to be invoked if the task execution exceeds the timeout limit.
apply_targetcallable = apply_targetThe internal wrapper used to execute the target function.

Returns

TypeDescription
greenlet.greenletThe spawned greenlet instance representing the scheduled task, augmented with a terminate method.

grow()

@classmethod
def grow(
n: int = 1
) - > null

Increases the capacity of the pool by a specified number of slots to allow more concurrent greenlets.

Parameters

NameTypeDescription
nint = 1The number of slots to add to the pool's current size.

Returns

TypeDescription
null

shrink()

@classmethod
def shrink(
n: int = 1
) - > null

Decreases the capacity of the pool by a specified number of slots to reduce concurrency.

Parameters

NameTypeDescription
nint = 1The number of slots to remove from the pool's current size.

Returns

TypeDescription
null

terminate_job()

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

Immediately kills a specific running task identified by its process ID (greenlet ID).

Parameters

NameTypeDescription
pidintThe unique identifier of the greenlet to be terminated.
signalint = NoneThe signal to send; note that GEvent uses greenlet.kill rather than OS signals.

Returns

TypeDescription
null

num_processes()

@classmethod
def num_processes() - > int

Returns the current number of active greenlets running in the pool.

Returns

TypeDescription
intThe count of active tasks currently managed by the pool.