Python 3.12+inspectfunction
inspect.getasyncgenlocals()
coroutines and generators
A dict of the current local variables of a suspended async generator. Added in 3.12.
Syntax
inspect.getasyncgenlocals(agen)
It works like getgeneratorlocals: before the first anext() the dict holds only the arguments, and after aclose() it is empty. Values are a snapshot at the current yield, and editing the dict does not change the generator. Pass the async generator object, not the function.
Examples
import asyncio, inspect
async def squares():
for i in range(1, 4):
sq = i * i
yield sq
async def main():
agen = squares()
print(inspect.getasyncgenlocals(agen))
await anext(agen)
print(inspect.getasyncgenlocals(agen))
await agen.aclose()
asyncio.run(main()) # → {} / {'i': 1, 'sq': 1}
See also
Open in the full glossarydocs.python.orgReport an inaccuracy