Programming Search⌘K

Featureadvanced

async for and async with

Also called async for, async with, asynchronous iterator, asynchronous iterable, asynchronous context manager, __aiter__, __anext__, __aenter__, __aexit__, StopAsyncIteration, aiter, anext, asynccontextmanager.

async for loops over an asynchronous iterator and awaits __anext__ for each item. async with awaits __aenter__ and __aexit__ of an asynchronous context manager. Both work only in a coroutine.

Language
Python
Proposals
pep-492

Synopsis

import asyncio

class Countdown:
    def __init__(self, start):
        self.n = start

    def __aiter__(self):
        return self

    async def __anext__(self):
        if self.n == 0:
            raise StopAsyncIteration
        await asyncio.sleep(0)
        self.n -= 1
        return self.n + 1

class Session:
    async def __aenter__(self):
        print("open")
        return self

    async def __aexit__(self, exc_type, exc, tb):
        print("close")
        return False

async def main():
    async with Session():
        async for n in Countdown(3):
            print(n)

asyncio.run(main())
Output
open
3
2
1
close

Semantics

r1. "An asynchronous iterable provides an aiter method that directly returns an asynchronous iterator, which can call asynchronous code in its anext method."1

r2. __anext__ "Must return an awaitable resulting in a next value of the iterator. Should raise a StopAsyncIteration error when the iteration is over." __aiter__() "must return an asynchronous iterator object. Returning anything else will result in a TypeError error."2 Example: aiter-coroutine.

r3. async for TARGET in ITER: SUITE else: SUITE2 is like a loop that calls iter = (ITER).__aiter__() once and then TARGET = await iter.__anext__() for each item, until StopAsyncIteration. The else part runs when the loop ends that way, as in a for loop.1 Example: by-hand.

r4. "An asynchronous context manager is a context manager that is able to suspend execution in its enter and exit methods."1 __aenter__ and __aexit__ are "Semantically similar" to __enter__ and __exit__, "the only difference being that it must return an awaitable."2

r5. async with EXPRESSION as TARGET: SUITE awaits __aenter__() and binds its result to TARGET. If the suite raises, it awaits __aexit__(*sys.exc_info()), and the exception is raised again unless that result is true. Otherwise it awaits __aexit__(None, None, None).1 Example: suppress.

r6. "It is a SyntaxError to use an async for statement outside the body of a coroutine function." The same is true for async with.1

r7. aiter(x) returns x.__aiter__(). anext(it) and anext(it, default) return an awaitable that gives the next item; with a default, the default comes back when the iterator is done.3 Example: by-hand.

r8. @contextlib.asynccontextmanager makes an asynchronous context manager from an asynchronous generator function, "without needing to create a class or separate aenter() and aexit() methods."4 Example: context-decorator.

Examples

import asyncio

class Letters:
    def __init__(self, text):
        self.chars = list(text)

    def __aiter__(self):
        return self

    async def __anext__(self):
        if not self.chars:
            raise StopAsyncIteration
        return self.chars.pop(0)

async def main():
    it = aiter(Letters("ab"))
    print(await anext(it))
    print(await anext(it))
    print(await anext(it, "done"))

asyncio.run(main())
Output
a
b
done

This is what async for does, one step at a time (rules r3 and r7).

import asyncio

class Ignore:
    def __init__(self, kind):
        self.kind = kind

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc, tb):
        if exc_type is not None and issubclass(exc_type, self.kind):
            print("ignored:", exc)
            return True
        return False

async def main():
    async with Ignore(KeyError):
        {}["missing"]
    print("still running")

asyncio.run(main())
Output
ignored: 'missing'
still running

__aexit__ got the exception and returned True, so the exception stopped there (rule r5).

import asyncio
from contextlib import asynccontextmanager

@asynccontextmanager
async def connection(name):
    print("connect", name)
    try:
        yield name.upper()
    finally:
        print("disconnect", name)

async def main():
    async with connection("db") as conn:
        print("using", conn)

asyncio.run(main())
Output
connect db
using DB
disconnect db

Pitfalls

This code raises TypeError, on purpose.

import asyncio

async def main():
    async for x in [1, 2, 3]:
        print(x)

asyncio.run(main())

A list has __iter__, not __aiter__, so async for cannot use it (rule r1). Use a plain for for a list.

This code raises TypeError, on purpose.

import asyncio

class Wrong:
    async def __aiter__(self):
        return self

    async def __anext__(self):
        raise StopAsyncIteration

async def main():
    async for x in Wrong():
        pass

asyncio.run(main())

__aiter__ must return the iterator itself, not a coroutine. Write it with def, not async def (rule r2).

This code raises TypeError, on purpose.

import asyncio
from contextlib import asynccontextmanager

@asynccontextmanager
async def resource():
    yield 1

async def main():
    with resource() as r:
        print(r)

asyncio.run(main())

An asynchronous context manager has __aenter__ and __aexit__, not __enter__ and __exit__, so a plain with cannot use it (rule r4).

Comparison

async for and async with mirror for and with: the special methods have an a in front of their names, and the interpreter awaits them (rules r3 and r5). An asynchronous generator, written with async def and yield, is the short way to write an asynchronous iterator; see Asynchronous generators.

References

  1. The Python Language Reference, Python 3.15, 8.9.2 The async for statement; 8.9.3 The async with statement
  2. The Python Language Reference, Python 3.15, 3.4.3 Asynchronous Iterators; 3.4.4 Asynchronous Context Managers
  3. Built-in Functions, Python 3.15 documentation, aiter(); anext()
  4. contextlib — Utilities for with-statement contexts, Python 3.15 documentation, asynccontextmanager

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source