asyncx.acontext

asyncx.acontext(coro: Awaitable[TReturn]) AsyncContextManager[asyncio.Task[TReturn]][source]
asyncx.acontext(future: TFuture) AsyncContextManager[TFuture]

Creates an async context manager that cancels a given awaitable in __aexit__.

Example

>>> async with asyncx.acontext(asyncio.sleep(1)) as t:
...     await asyncio.sleep(0.1)
>>> assert t.cancelled()

Example

>>> async with contextlib.AsyncExitStack() as stack:
...     await stack.enter_async_context(asyncio.sleep(0.1))
...     await stack.enter_async_context(asyncio.sleep(0.2))
>>>
Parameters

awaitable – A target awaitable object

Returns

An async context manager that creates a future and cancels it of a given awaitable.