Language Syntax

Async for — Python syntax

Iterate by awaiting an asynchronous iterator's next operation until StopAsyncIteration. Valid inside an async function; break skips the else suite. Ordinary synchronous iterables are not automatically converted. The else suite is optional.

Formal Syntax & Structure

python syntaxpython
async for target in asynchronous_iterable:    suiteelse:    completion_suite

Working Example

Example Snippetpython
import asyncioasync def values():    yield 1    yield 2async def run():    out=[]    async for x in values():        out.append(x)    else:        out.append(3)    return outprint(asyncio.run(run()))
Python Reference