Python 3.12+inspectfunction
inspect.getasyncgenstate()
coroutines and generators
An async generator's state as a string: AGEN_CREATED, AGEN_RUNNING, AGEN_SUSPENDED or AGEN_CLOSED. Added in 3.12.
Syntax
inspect.getasyncgenstate(agen)
An async generator is an async def with yield, and neither getgeneratorstate nor getcoroutinestate applies to it. Close it with await agen.aclose(), not close(): async generators have no synchronous close(). Before 3.12 you had to derive the state from ag_running and ag_frame by hand.
Examples
import asyncio, inspect
async def numbers():
yield 1
yield 2
async def main():
agen = numbers()
print(inspect.getasyncgenstate(agen))
print(await anext(agen), inspect.getasyncgenstate(agen))
await agen.aclose()
print(inspect.getasyncgenstate(agen))
asyncio.run(main()) # → AGEN_CREATED / 1 AGEN_SUSPENDED / AGEN_CLOSED
See also
Open in the full glossarydocs.python.orgReport an inaccuracy