Python Glossary Русский

Python 3.6+inspectfunction

inspect.isasyncgen()

coroutines and generators

Checks that an object is an asynchronous generator iterator: the result of calling an async def function with yield.

Syntax

inspect.isasyncgen(object) -> bool

Calling such a function runs nothing and immediately returns an object for async for; it has __anext__, not __await__, so isawaitable and iscoroutine give False. A plain for loop over it fails with TypeError. An unfinished async generator is best closed explicitly (await agen.aclose()) or through contextlib.aclosing, otherwise its finally block runs at some unknown point.

Examples

import inspect
async def ticks():
    yield 1
agen = ticks()
print(inspect.isasyncgen(agen), inspect.isasyncgen(ticks))  # → True False
print(inspect.isawaitable(agen), inspect.iscoroutine(agen))  # → False False
try:
    for x in agen:
        pass
except TypeError as e:
    print(type(e).__name__)  # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy