Async Standard Library (asyncstdlib)

3.14.0 · active · verified Fri Apr 10

asyncstdlib provides async equivalents for functions and classes found in Python's standard library modules like itertools, functools, contextlib, builtins, and os. It aims to make writing asynchronous code more concise and familiar by mirroring the API of their synchronous counterparts. The current version is 3.14.0, maintaining feature parity with Python 3.14. New major versions are typically released to align with new Python releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using `asyncstdlib`'s async versions of `map`, `filter`, and `anext` with an async generator. It highlights how to apply common iteration patterns in an asynchronous context.

import asyncio
import asyncstdlib as a

async def async_counter(limit: int):
    """An async generator that yields numbers up to a limit."""
    for i in range(limit):
        await asyncio.sleep(0.01) # Simulate async work
        yield i

async def main():
    print("\nMapping values (doubling):")
    doubled_values = a.itertools.map(lambda x: x * 2, async_counter(5))
    async for item in doubled_values:
        print(item)

    print("\nFiltering values (evens only):")
    even_values = a.itertools.filter(lambda x: x % 2 == 0, async_counter(6))
    async for item in even_values:
        print(item)

    print("\nConsuming with anext:")
    it = a.builtins.aiter(async_counter(3))
    print(await a.builtins.anext(it))
    print(await a.builtins.anext(it))

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →