Python 3.6+sysfunction
sys.get_asyncgen_hooks()
asynchronous code
Returns the asynchronous generator hooks (firstiter, finalizer) installed by the event loop — to close abandoned async generators.
Syntax
sys.get_asyncgen_hooks()
Outside an event loop both fields are None; inside asyncio.run() asyncio installs them. That is why an async generator abandoned mid async for does not lose its finally: the finalizer hook schedules its aclose() on the event loop, and loop.shutdown_asyncgens() closes any still alive at the end. Application code does not need these hooks — they are for people writing their own event loops.
Examples
import asyncio, sys
print(sys.get_asyncgen_hooks().firstiter) # → None
async def main():
hooks = sys.get_asyncgen_hooks()
print(hooks.firstiter is not None, hooks.finalizer is not None)
asyncio.run(main()) # → True True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy