Content · Programming · Python

Python Async Generators and async yield

An async generator combines async def with yield. It emits one item at a time and may await I/O between items, so a caller uses async for rather than an ordinary for loop.

2 views Published Oct 18, 2026 Updated Oct 18, 2026 Admin

An async generator combines async def with yield. It emits one item at a time and may await I/O between items, so a caller uses async for rather than an ordinary for loop.

What You Will Learn


This lesson focuses on Async Generators and async yield. You will connect the operation to a concrete input, inspect the result and identify the situation in which a different approach would be safer or clearer.

Asynchronous programming is useful when a program spends time waiting for other work, such as a socket or a remote service. One event loop can keep several operations in progress, provided each operation yields at an appropriate await point.

How It Works


Unlike a regular generator, an async generator is consumed through the asynchronous iteration protocol. Wrap long-lived streams in contextlib.aclosing when deterministic cleanup matters.

Worked Example


The following example isolates the main behavior so you can run it from a local Python installation. Read the inputs first, then follow the operation and the final printed result.

import asyncio
async def statuses():
    for label in ('queued', 'running', 'finished'):
        await asyncio.sleep(0)
        yield label
async def main():
    async for status in statuses():
        print(status)
asyncio.run(main())

Understanding the Example


The async def function creates an asynchronous generator. Each yield transfers a value to the caller; await lets another task run before the next value. asyncio.run starts the event loop used by async for.

The important behavior demonstrated here is: An async generator combines async def with yield. It emits one item at a time and may await I/O between items, so a caller uses async for rather than an ordinary for loop. The example uses deliberately small values so the intermediate result is easy to reason about. A larger application must still handle validation, errors and resource cleanup.

Example Output


queued
running
finished

This output was obtained by running the local example in the available Python environment. Verify that the values match your reasoning, then change one input and try again.

When to Use This Approach


Use the design when the underlying operation is awaitable or when integrating with a well-defined async interface. For a single straightforward blocking task, regular synchronous code can be easier to maintain.

Important Behavior and Edge Cases


Calling next() on an async generator or expecting its body to run before iteration begins.

Own the lifetime of tasks. Bound concurrency, observe exceptions, set appropriate deadlines and close resources after cancellation. A synchronous blocking function can still freeze an event loop.

Practice Exercise


Extend the stream with a fourth status and stop after three items.

Before running your modified code, predict the output or expected failure. Confirm the result using at least one ordinary case and one invalid, missing or boundary input. If the operation accesses external resources, first exercise it against a controlled local or test environment.

Common Beginner Mistakes


  • Calling next() on an async generator or expecting its body to run before iteration begins.
  • Own the lifetime of tasks. Bound concurrency, observe exceptions, set appropriate deadlines and close resources after cancellation. A synchronous blocking function can still freeze an event loop.

Key Takeaways


  • An async generator combines async def with yield. It emits one item at a time and may await I/O between items, so a caller uses async for rather than an ordinary for loop.
  • Choose this technique for the problem it solves, but validate inputs and follow the cleanup and failure behavior shown above.
  • Next practice step: Extend the stream with a fourth status and stop after three items.
Share this resourceSend this page to someone who may find it useful.