TaskPool
GEvent Pool.
Attributes
| Attribute | Type | Description |
|---|---|---|
| Timer | class = Timer | Reference to the Timer class used for scheduling timed events within the pool. |
| signal_safe | boolean = false | Boolean flag indicating if the pool can safely handle POSIX signals, set to False for gevent compatibility. |
| is_green | boolean = true | Boolean flag indicating that this pool implementation uses greenlets for concurrency. |
| task_join_will_block | boolean = false | Indicates whether joining a task will block the current execution thread. |
Constructor
Signature
def TaskPool(
*args: any,
**kwargs: any
)
Parameters
| Name | Type | Description |
|---|---|---|
| *args | any | Variable length argument list passed to the base class constructor. |
| **kwargs | any | Arbitrary 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
| Type | Description |
|---|---|
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
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| target | callable | The function or task to be executed within the pool. |
| args | list = None | Positional arguments to pass to the target function. |
| kwargs | dict = None | Keyword arguments to pass to the target function. |
| callback | callable = None | Function to be invoked upon successful completion of the task. |
| accept_callback | callable = None | Function to be invoked when the task is accepted by the pool. |
| timeout | float = None | Maximum time in seconds allowed for task execution before it is aborted. |
| timeout_callback | callable = None | Function to be invoked if the task execution exceeds the timeout limit. |
| apply_target | callable = apply_target | The internal wrapper used to execute the target function. |
Returns
| Type | Description |
|---|---|
greenlet.greenlet | The 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
| Name | Type | Description |
|---|---|---|
| n | int = 1 | The number of slots to add to the pool's current size. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| n | int = 1 | The number of slots to remove from the pool's current size. |
Returns
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| pid | int | The unique identifier of the greenlet to be terminated. |
| signal | int = None | The signal to send; note that GEvent uses greenlet.kill rather than OS signals. |
Returns
| Type | Description |
|---|---|
null |
num_processes()
@classmethod
def num_processes() - > int
Returns the current number of active greenlets running in the pool.
Returns
| Type | Description |
|---|---|
int | The count of active tasks currently managed by the pool. |