This module provides utilities for running functions in thread-pool (or other) executors while preservingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt
Use this file to discover all available pages before exploring further.
contextvars.ContextVar state, bridging between sync and async contexts, and detecting unhandled exceptions in fire-and-forget tasks.
exec_in_executor
Runs a synchronous or asynchronous function in an executor, preserving the current ContextVar snapshot, and returns an asyncio.Future.
- The
executorargument if notNone. - The executor passed to
initConfig(executor=...)if set. None— asyncio’s default executor.
The
concurrent.futures.Executor to run the function in. Pass None to use the configured global or asyncio default.The function or coroutine function to execute. Both sync and
async def functions are accepted.*args
Positional arguments forwarded to
func.**kwargs
Keyword arguments forwarded to
func.asyncio.Future[T] — wrapped in a FutureWrapper that logs unconsumed exceptions automatically.
Each thread in the executor receives an event loop via
ensure_thread_event_loop(), so async functions submitted to the executor can themselves schedule coroutines.exec_in_executor_threading_future
Same semantics as exec_in_executor, but returns a concurrent.futures.Future (a threading.Future) instead of an asyncio.Future. Useful when you need to block a non-async thread waiting for a result.
exec_in_executor.
AsyncExecutionQueue
An async context manager that manages a background worker task. Tasks submitted to the queue are executed in order through exec_in_executor, preserving ContextVar state at the time of submission.
Constructor parameters
Custom queue to use. A new
asyncio.Queue() is created if omitted.Executor to run tasks. Falls back to the global executor or asyncio default.
aadd_task(func, /, *args, **kwargs) -> asyncio.Future
Asynchronously enqueues a task. The current ContextVar context is captured at submission time and restored when the task runs. Returns an asyncio.Future that resolves to the function’s return value.
add_task(executor, func, /, *args, **kwargs) -> threading.Future
Synchronous counterpart to aadd_task. Can be called from a non-async thread. Returns a concurrent.futures.Future.
aclose()
Cancels all pending tasks, signals the worker to exit, and waits for it to finish. Called automatically by __aexit__.
lift_to_async
Calls an async function from synchronous code, preserving ContextVar state. Dispatches to the event loop captured by initConfig().
The async function to call. Passed as a positional-only argument.
*args
Positional arguments forwarded to
afunc.**kwargs
Keyword arguments forwarded to
afunc.ensure_thread_event_loop
Initialises an event loop for the current thread if one does not already exist, and stores it in thread-local storage.
exec_in_executor for each worker thread, so you typically do not need to call it directly.
FutureWrapper
A lightweight proxy around an asyncio.Future that tracks whether its result was ever consumed (via await, .result(), or .exception()). If the future completes with an exception and nobody consumed it, the exception is logged automatically.
| Member | Description |
|---|---|
__await__ | Marks as consumed and delegates to the underlying future. |
result() | Marks as consumed and returns the result. |
exception() | Marks as consumed and returns the exception. |
add_done_callback(cb) | Delegates to the underlying future. |
done() | Delegates to the underlying future. |
_consumed | bool — True once the result has been accessed. |
chain_future_results
Propagates the result or exception from a source future to a target future. Typically used as a done callback.
The future whose outcome will be read.
The future that will receive the result or exception. No-ops if it is already resolved.
handle_future_exception
Schedules a delayed check that logs any unconsumed exception from a future. Used internally by exec_in_executor but can be called directly for custom futures.
The future to monitor.
Seconds to wait before checking. Defaults to
0 (next iteration of the event loop).