aioitertools: itertools and builtins for AsyncIO

raw JSON →
0.13.0 verified Tue May 12 auth: no python install: verified quickstart: verified

aioitertools provides asynchronous versions of standard library `itertools` and `builtins` for AsyncIO and mixed iterables. It offers a unified interface for both standard and async iterators, wrapping standard iterables in async generators. The current version is 0.13.0, and it follows a feature release cadence.

pip install aioitertools
error ModuleNotFoundError: No module named 'aioitertools'
cause The 'aioitertools' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install aioitertools'.
error ImportError: cannot import name 'iter' from 'aioitertools'
cause The 'iter' function is not available in the 'aioitertools' module.
fix
Use the standard 'iter' function from Python's built-in library instead.
error TypeError: 'async for' requires an object with an __aiter__ method, got 'list'
cause Attempting to use 'async for' on a standard list, which is not an asynchronous iterable.
fix
Convert the list to an asynchronous iterable using 'aioitertools.iter()' before iterating.
error TypeError: 'async_generator' object is not callable
cause You are attempting to call an aioitertools asynchronous generator function as if it were a regular synchronous function, or trying to retrieve a single item from an async iterator without using 'await'.
fix
Ensure you use 'await' when retrieving single items from async iterators (e.g., await ait.next(it)) and ensure the function itself is awaited if it returns a single awaitable result. If it's an async generator, use 'async for' to iterate over it.
error TypeError: 'async_generator' object is not iterable
cause You are trying to iterate over an asynchronous generator returned by an aioitertools function using a standard (synchronous) 'for' loop, instead of an 'async for' loop.
fix
Use 'async for' to iterate over the asynchronous generator within an 'async def' function. For example, async for item in ait.map(func, data):.
deprecated The `loop` parameter to `aioitertools.asyncio` functions was deprecated in v0.9.0 and completely removed in v0.11.0. This aligns with modern `asyncio` practices.
fix Remove the `loop` argument when calling any `aioitertools.asyncio` functions. The event loop is now managed automatically.
breaking Support for Python 3.6 and 3.7 was dropped in version 0.12.0.
fix Upgrade your Python environment to version 3.8 or newer.
gotcha `aioitertools.asyncio.as_completed` yields actual results of awaitables, unlike the standard `asyncio.as_completed` which yields futures.
fix Be aware that `aioitertools.asyncio.as_completed` directly gives you the resolved values, so you do not need to `await` on the yielded items.
gotcha Functions like `aioitertools.permutations` or `aioitertools.combinations` may consume the entire input iterable into memory before yielding any results. This can be inefficient for very large or infinite iterables.
fix For very large iterables, consider if these functions are suitable, or use them with a limited subset of data. Be mindful of memory usage.
gotcha Similar to standard `itertools`, infinite iterators (e.g., `aioitertools.count`, `aioitertools.cycle`) will run indefinitely.
fix Always combine infinite iterators with a 'stopping' function, such as `aioitertools.islice`, `aioitertools.takewhile`, or an explicit `break` condition in an `async for` loop, to prevent unbounded iteration.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.09s 18.0M
3.10 slim (glibc) - - 0.07s 19M
3.11 alpine (musl) - - 0.17s 19.9M
3.11 slim (glibc) - - 0.14s 20M
3.12 alpine (musl) - - 0.38s 11.8M
3.12 slim (glibc) - - 0.35s 12M
3.13 alpine (musl) - - 0.40s 11.4M
3.13 slim (glibc) - - 0.34s 12M
3.9 alpine (musl) - - 0.10s 17.8M
3.9 slim (glibc) - - 0.08s 18M

This quickstart demonstrates how to use `aioitertools.iter` to get an async iterator from both synchronous and asynchronous iterables, `aioitertools.next` to retrieve the next item, and `aioitertools.map` to apply an asynchronous function concurrently over an iterable.

import asyncio
from aioitertools import iter, next, map

async def async_generator():
    for i in range(3):
        yield i
        await asyncio.sleep(0.01)

async def process_item(item):
    # Simulate an async operation
    await asyncio.sleep(0.005)
    return item * 2

async def main():
    # Using aioitertools.iter with a standard iterable
    it_sync = iter([1, 2, 3])
    first_item = await next(it_sync)
    print(f"First item from sync: {first_item}")

    # Using aioitertools.iter with an async iterable
    it_async = iter(async_generator())
    print("Items from async generator:")
    async for item in it_async:
        print(f"  {item}")

    # Using aioitertools.map for concurrent async operations
    urls = ["url1", "url2", "url3"]
    async def fetch_url(url):
        # Replace with actual async HTTP request
        await asyncio.sleep(0.01)
        return f"Processed {url}"

    print("\nMapping async functions:")
    async for result in map(fetch_url, urls):
        print(f"  {result}")

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