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
| Attribute | Type | Description |
|---|---|---|
| Timer | class = Timer | Reference to the Timer class used for scheduling internal events. |
| signal_safe | boolean = False | Boolean flag indicating if the pool can safely handle POSIX signals, set to False for eventlet. |
| is_green | boolean = True | Boolean flag indicating that this pool implementation uses green threads. |
| task_join_will_block | boolean = False | Boolean flag indicating whether joining a task will block the entire process. |
Constructor
Signature
def TaskPool(
*args: tuple,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| *args | tuple | Variable length argument list passed to the base class constructor. |
| **kwargs | dict | Arbitrary 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
| Name | Type | Description |
|---|---|---|
| target | callable | The function or task to be executed asynchronously |
| 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 called with the result upon successful task completion |
| accept_callback | callable = None | Function 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
| Name | Type | Description |
|---|---|---|
| n | int = 1 | The 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
| Name | Type | Description |
|---|---|---|
| n | int = 1 | The 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
| Name | Type | Description |
|---|---|---|
| pid | int | The unique identifier of the greenlet to be terminated |
| signal | int = None | The signal to send; note that Eventlet greenlets primarily respond to kill() rather than system signals |