Undocumented
Class |
|
Undocumented |
Class |
|
Undocumented |
Class |
|
Undocumented |
Class |
|
Undocumented |
Function | asyncstartablecontext |
@asyncstartablecontext decorator. |
Type Variable | _PT |
Undocumented |
Type Variable | _T |
Undocumented |
Type Variable | _ |
Undocumented |
Callable[ ..., AsyncIterator[ _T_co]]
) -> Callable[ ..., GeneratorStartableContext[ _T_co]]
:
(source)
¶
@asyncstartablecontext decorator. the decorated function can be called either as ``async with fn()``, **or** ``await fn()``. This is decidedly different from what ``@contextlib.asynccontextmanager`` supports, and the usage pattern is different as well. Typical usage:: @asyncstartablecontext async def some_async_generator(<arguments>): <setup> try: yield <value> except GeneratorExit: # return value was awaited, no context manager is present # and caller will .close() the resource explicitly pass else: <context manager cleanup> Above, ``GeneratorExit`` is caught if the function were used as an ``await``. In this case, it's essential that the cleanup does **not** occur, so there should not be a ``finally`` block. If ``GeneratorExit`` is not invoked, this means we're in ``__aexit__`` and we were invoked as a context manager, and cleanup should proceed.