Wrap a blocking sync context manager in a non-blocking async context manager.
| 32 | |
| 33 | |
| 34 | class SyncResource(contextlib.AbstractAsyncContextManager[T], typing.Generic[T]): |
| 35 | """Wrap a blocking sync context manager in a non-blocking async context manager.""" |
| 36 | |
| 37 | def __init__(self, cm: typing.ContextManager[T]): |
| 38 | self.sync_cm = cm |
| 39 | |
| 40 | async def __aenter__(self) -> T: |
| 41 | return await asyncify(self.sync_cm.__enter__) |
| 42 | |
| 43 | async def __aexit__(self, *exc_details) -> None: |
| 44 | await asyncify(self.sync_cm.__exit__, *exc_details) |