async_executors
IAsyncExecutor
submit
abstractmethod
Submit a coroutine to the executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
FutureLike
|
A Future object representing the execution of the coroutine. |
run
Run a coroutine in the executor and block until it completes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result of the coroutine. |
run_multiple_async
async
run_multiple_async(tasks: List[Awaitable[Any]], max_workers: int = 10, timeout: float = 10) -> AsyncGenerator[Tuple[Any, str], None]
Run multiple coroutines in parallel using the underlying executor. Limits the number of concurrent tasks to max_workers and applies a timeout to each task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
List[Awaitable[Any]]
|
A list of coroutines to run. |
required |
max_workers
|
int
|
The maximum number of concurrent tasks. |
10
|
timeout
|
float
|
The maximum time to wait for each individual task to complete in seconds. |
10
|
Yields:
| Type | Description |
|---|---|
AsyncGenerator[Tuple[Any, str], None]
|
A tuple of (result, error) for each task. |
run_multiple
run_multiple(tasks: List[Awaitable[Any]], max_workers: int = 10, timeout=10) -> Generator[Tuple[Any, str], None, None]
Run multiple coroutines in parallel using the underlying executor. Block calling thread and yield results as they complete.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
List[Awaitable[Any]]
|
A list of coroutines to run. |
required |
max_workers
|
int
|
The maximum number of concurrent tasks. |
10
|
timeout
|
The maximum time to wait for each individual task to complete in seconds. |
10
|
Yields:
| Type | Description |
|---|---|
Tuple[Any, str]
|
A tuple of (result, error) for each task. |
DefaultBackgroundExecutor
Bases: IAsyncExecutor
Maintains a single dedicated thread for an asyncio event loop.
submit
Submit a coroutine to the executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
FutureLike
|
A Future object representing the execution of the coroutine. |
EventLoopPoolExecutor
Bases: IAsyncExecutor
A utility class that manages a pool of persistent asyncio event loops, each running in its own dedicated thread. It load balances tasks among the event loops by tracking pending tasks and selecting the loop with the smallest load.
get_event_loop
Select an event loop from the pool based on current load (i.e., pending tasks).
Returns:
| Type | Description |
|---|---|
AbstractEventLoop
|
A tuple (selected_event_loop, index) where selected_event_loop is the least loaded |
int
|
asyncio.AbstractEventLoop and index is its index in the pool. |
submit
Submit a coroutine to the executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[Any]
|
The coroutine to execute. |
required |
Returns:
| Type | Description |
|---|---|
FutureLike
|
A Future object representing the execution of the coroutine. |