async for is a Python statement that loops over an asynchronous iterable, an object whose items are produced by awaiting. Each step of the loop can suspend while it waits for the next item, so the event loop can run other tasks in the meantime. It works only inside an async def function (or an asyncio REPL) and is the asynchronous counterpart of a regular for loop.
Use it for sources that deliver data over time, such as network streams, message queues, database cursors, or any async generator.
A regular for loop calls __iter__() and then __next__() on the iterable. An async for loop calls __aiter__() and then awaits __anext__() for every item. Because __anext__() is awaited, the loop body does not run until the next item is ready, and the event loop is free to run other coroutines while the loop waits.
| Feature | for | async for |
|---|---|---|
| Protocol methods | __iter__, __next__ | __aiter__, __anext__ |
| Where it can run | Anywhere | Only inside async def |
| Stop signal | StopIteration | StopAsyncIteration |
| Waits for items | Blocks the thread | Suspends the coroutine |
async for does not run iterations in parallel. Items are still processed one after another; the benefit is that waiting does not block other tasks.
The simplest asynchronous iterable is an async generator: an async def function that contains yield.
The generator pauses at each yield, and the loop in main resumes it by awaiting the next value. When the generator function returns, Python raises StopAsyncIteration internally and the loop ends cleanly.
You can also build an asynchronous iterator by hand. __aiter__ must return the iterator (it is a normal method, not a coroutine), and __anext__ must be an async def method that returns the next item or raises StopAsyncIteration.
In most code an async generator is shorter and easier to get right, so write a class only when the iterator needs extra state or methods.
The standard library open() is blocking, so asynchronous file access needs a third-party library such as aiofiles, which returns objects that support async for and async with.
Libraries for WebSockets, message brokers, and streaming HTTP responses usually expose their messages as async iterators. The pattern is always the same, shown here with a simulated stream.
Python 3.6 and later also allow async for inside comprehensions, as long as they appear in an async def function.
Since Python 3.10 you can also call the built-ins aiter() and anext() to drive an async iterator manually, for example first = await anext(aiter(countdown(3))).
async for outside an async def function raises a SyntaxError. Wrap the code in a coroutine and start it with asyncio.run().for over an async generator raises TypeError: 'async_generator' object is not iterable. Use async for instead.time.sleep() or requests.get()) inside the loop body freezes the whole event loop. Use await asyncio.sleep() or an async library, or move the work to a thread with asyncio.to_thread().contextlib.aclosing() when it holds resources: async with aclosing(gen()) as g: async for item in g: ....No. A list is a synchronous iterable and does not define __aiter__, so async for raises a TypeError. Use a regular for loop, or wrap the list in an async generator if an API requires it.
No. The loop still handles one item at a time. To process many items at once, create tasks with asyncio.TaskGroup (Python 3.11+) or asyncio.gather().
async for iterates over an asynchronous iterable, while async with manages setup and cleanup using an asynchronous context manager. They are often combined, as in the file example above.
async for has been available since Python 3.5. Async generators and async comprehensions arrived in 3.6, and aiter() and anext() in 3.10.
async for iterates over objects that produce items asynchronously by awaiting __anext__() at each step. Use async generators for most cases, implement __aiter__ and __anext__ only when you need a custom iterator, and keep blocking calls out of the loop body so the event loop stays responsive.