contextmanager_in_threadpool
Converts a synchronous context manager into an asynchronous context manager by executing its enter and exit methods within a threadpool to avoid blocking the event loop.
def contextmanager_in_threadpool(
cm: AbstractContextManager[_T]
) - > AsyncGenerator[_T, None]
Wraps a synchronous context manager to be used asynchronously by executing its enter and exit logic within a separate thread pool. This allows blocking context managers to be used in async code without stalling the event loop, using a dedicated capacity limiter for the exit phase to prevent deadlocks.
Parameters
| Name | Type | Description |
|---|---|---|
| cm | AbstractContextManager[_T] | The synchronous context manager instance to be executed within the thread pool. |
Returns
| Type | Description |
|---|---|
AsyncGenerator[_T, None] | An asynchronous generator that yields the resource managed by the synchronous context manager. |