Python Glossary Русский

Python 3.6+inspectfunction

inspect.isasyncgenfunction()

coroutines and generators

Checks that an object is an asynchronous generator function: an async def whose body contains yield.

Syntax

inspect.isasyncgenfunction(object) -> bool

Such a function is not a coroutine function: iscoroutinefunction gives False, and its result cannot be awaited; you iterate it with async for. The mix-up is easy when a decorator checks only iscoroutinefunction: it lets an async generator through as a plain function. isgeneratorfunction is False for it too.

Examples

import asyncio, inspect
async def ticks(n):
    for i in range(n):
        yield i
print(inspect.isasyncgenfunction(ticks))  # → True
print(inspect.iscoroutinefunction(ticks), inspect.isgeneratorfunction(ticks))  # → False False
async def main():
    return [i async for i in ticks(3)]
print(asyncio.run(main()))  # → [0, 1, 2]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy