Python 3.6+collectionsterm
collections.abc.AsyncGenerator
collections.abc — protocols
The async generator protocol: an async iterator with asend/athrow/aclose.
Syntax
isinstance(obj, collections.abc.AsyncGenerator)
The check is structural: nothing has to inherit from the ABC, having __aiter__, __anext__, asend, athrow and aclose is enough, so a hand-rolled class with those methods passes too. The real gotcha is the object itself — calling ag() runs no code, the body only starts under async for inside a running event loop, and breaking out of that loop leaves finally blocks pending, so close it explicitly with aclose() or contextlib.aclosing.
Examples
import collections.abc as abc
async def ag():
yield 1
print(isinstance(ag(), abc.AsyncGenerator)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy