Async Standard Library (asyncstdlib)
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
- gotcha Mixing `asyncstdlib` functions with built-in or synchronous standard library functions (e.g., `itertools.map` on an async iterable) will lead to `TypeError` or incorrect behavior, as asyncstdlib functions expect and return async iterables/awaitables.
- breaking In `v3.13.3`, the signatures and behavior of `asyncstdlib.itertools.islice` and `asyncstdlib.itertools.accumulate` were updated to match their synchronous `itertools` counterparts more closely. This primarily affects `islice` overloads and `accumulate`'s `initial` parameter, potentially causing subtle breaking changes if your code relied on previous divergent behavior.
- gotcha As of `v3.13.3`, `asyncstdlib.itertools.tee` instances now share an internal buffer for improved efficiency, mirroring `itertools.tee` in CPython. While generally a performance enhancement, this can subtly change behavior if you were relying on independent buffering or specific mutation patterns of yielded items across `tee` branches in earlier versions.
- deprecated `asyncstdlib` versions from `3.12.1` onwards no longer support Python 3.6 and 3.7. The `requires_python` specification is `~=3.8`.
Install
-
pip install asyncstdlib
Imports
- map
from asyncstdlib.itertools import map as amap
- anext
from asyncstdlib.builtins import anext
- AsyncStandardLibrary
import asyncstdlib as a
Quickstart
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())