Skip to content

async_executors

TaskInfo

TaskInfo(future: FutureLike, event_loop: AbstractEventLoop = None, thread: Thread = None)

TaskResult

TaskResult(success: bool, output: Any = None, exception: Exception = None)

IAsyncExecutor

submit abstractmethod

submit(coro: Awaitable[Any]) -> FutureLike

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(coro: Awaitable[Any]) -> Any

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.

wrap_async_iterable

wrap_async_iterable(async_iterable: AsyncIterable) -> Iterable

Wraps an AsyncIterable into a sync Iterable by driving async iterator with executors runs.

DefaultBackgroundExecutor

DefaultBackgroundExecutor()

Bases: IAsyncExecutor

Maintains a single dedicated thread for an asyncio event loop.

submit

submit(coro: Awaitable[Any]) -> FutureLike

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.

shutdown

shutdown(wait: bool = True) -> None

Shutdown the executor and stop the event loop.

Parameters:

Name Type Description Default
wait bool

If True, block until the thread is terminated.

True

EventLoopPoolExecutor

EventLoopPoolExecutor(pool_size: int = 1)

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

get_event_loop() -> Tuple[asyncio.AbstractEventLoop, int]

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(coro: Awaitable[Any]) -> FutureLike

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.

shutdown

shutdown(wait: bool = True) -> None

Shutdown all event loops and join their threads.

Parameters:

Name Type Description Default
wait bool

If True, block until all threads are terminated.

True