Skip to main content

The async_generator library

Python’s iterators are great to use – but manually implementing the iterator protocol (__iter__, __next__) can be very annoying. No-one wants to do that all the time.

Fortunately, Python has generators, which make it easy and straightforward to create an iterator by writing a function. E.g., if you have a file where each line is a JSON document, you can make an iterator over the decoded bodies with:

def load_json_lines(fileobj):
    for line in fileobj:
        yield json.loads(line)

Starting in v3.5, Python has added *async iterators* and *async functions*. These are like regular iterators and functions, except that they have magic powers that let them do asynchronous I/O without twisting your control flow into knots.

Asynchronous I/O code is all about incrementally processing streaming data, so async iterators are super handy. But manually implementing the async iterator protocol (__aiter__, __anext__) can be very annoying, which is why we want async generators, which make it easy to create an async iterator by writing an async function. For example, suppose that in our example above, we want to read the documents from a network connection, instead of the local filesystem. Using the asyncio.StreamReader interface we can write:

async def load_json_lines(asyncio_stream_reader):
    async for line in asyncio_stream_reader:
        async yield json.loads(line)

BUT! the above DOESN’T WORK in Python 3.5 – you just get a syntax error. In 3.5, the only way to make an async generator is to manually define __aiter__ and __anext__.

Until now.

This is a little library which implements async generators in Python 3.5, by emulating the above syntax. The two changes are that you have to decorate your async generators with @async_generator, and instead of writing async yield x you write await yield_(x):

# Same example as before, but works in Python 3.5
from async_generator import async_generator, yield_

@async_generator
async def load_json_lines(asyncio_stream_reader):
    async for line in asyncio_stream_reader:
        await yield_(json.loads(line))

Semantics

In addition to being super handy, this library is intended to help us get some experience with writing and using async generators, so that we can figure out exactly what semantics they should have, in hopes of getting them added to the language properly in ~3.6/3.7. So here are some notes on the exact design decisions taken (for now):

send, throw, yield from

We do not implement any async equivalent to regular generators’

send and throw. await yield_(...) always returns None.

We do not implement any kind of async yield from ....

This isn’t because these features are uninteresting. Recall that yield from isn’t just useful as a trick for implementing async coroutines – it was originally motivated as a way to allow complex generators to be refactored into multiple pieces, and that rationale applies equally for async generators. It’s not as clear whether asynchronous asend(...) and athrow(...) methods are really compelling – though one use case would be that if we start implementing network protocols as async generators that do async for ... to read and async yield ... to write, then it is nice if the write operation has a way to signal an error!

But, there are two reasons why we nonetheless leave these out (for now): (a) It’d be easy for this library to implement either one of these features, but implementing both of them is substantially more difficult, and it isn’t clear which is more useful. (b) It might be extremely difficult for CPython to natively implement either of these features at all, and we don’t want that to block getting the basic feature implemented. By leaving these features out we can get some data on just how useful they really are.

close

We do not implement any equivalent to the generator close method. We are currently trying to figure out whether or not this is a bug.

Metadata

Release files for async_generator 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async_generator 0.0.1
File Size Uploaded Provenance Actions
async_generator-0.0.1.tar.gz 6.1 kB Without provenance Details for async_generator-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for async_generator 0.0.1
File Interpreter ABI Platform Actions
async_generator-0.0.1-py3-none-any.whl Python 3 none any Details for async_generator-0.0.1-py3-none-any.whl

Total release size: 13.0 kB

Release files / async_generator-0.0.1.tar.gz

Download URL async_generator-0.0.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
97395f027c3dbe5de9a8a87bfd4f9110d9408f904e55037ed18c9c1c9b419294
BLAKE2b-256 checksum
How to use checksums
b30d6373b24c977b6f04e6a7af031d02939956c1ad0933fe29174d583a3d3956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / async_generator-0.0.1-py3-none-any.whl

Download URL async_generator-0.0.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9aac800f525cd566ce67cfc1765377d56148a6801a3439e789a2c879f59434db
BLAKE2b-256 checksum
How to use checksums
5b97a42f36ab6be2de9db06752ff2c67ffa3ca4c551c0b6cceca89b93ae35528
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for async_generator 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page