Python 3.5+Exceptionsexception
StopAsyncIteration
built-in exceptions
Signals that an async iterator is exhausted (async analog of StopIteration).
Syntax
raise StopAsyncIteration
You almost never raise this yourself — an async iterator's __anext__ raises it, and async for swallows it and ends the loop. Inside an async generator, raising StopAsyncIteration to finish is wrong: the interpreter replaces it with RuntimeError, so use a plain return. A synchronous for or next() will not recognise the signal at all; it only means anything in async code.
Examples
try:
raise StopAsyncIteration
except StopAsyncIteration as e:
print(type(e).__name__) # → StopAsyncIteration
See also
Open in the full glossarydocs.python.orgReport an inaccuracy