aiometer: Concurrency Scheduler

1.0.0 · active · verified Thu Apr 16

aiometer is a Python concurrency scheduling library, compatible with asyncio and trio. It makes it easier to execute many tasks concurrently while controlling concurrency limits and collecting results predictably. It is currently at version 1.0.0 and has a consistent release cadence with a focus on supporting recent Python and `anyio` versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `aiometer.run_all` for ordered results and `aiometer.amap` for results as they become available. It also showcases applying concurrency limits (`max_at_once`, `max_per_second`) and how to adapt functions requiring multiple arguments using `functools.partial` for `aiometer`'s single-argument functions.

import asyncio
import functools
import aiometer

async def get_greeting(name: str) -> str:
    """Simulates an async operation, returning a greeting."""
    await asyncio.sleep(0.05) # Simulate I/O
    return f"Hello, {name}!"

async def main():
    names = ["Alice", "Bob", "Charlie", "David", "Eve"]

    print("Running tasks with aiometer.run_all (ordered results):")
    # Use functools.partial to pass multiple arguments or wrap complex logic
    greetings_ordered = await aiometer.run_all(
        [functools.partial(get_greeting, name) for name in names],
        max_at_once=2, # Limit to 2 concurrent tasks
        max_per_second=5 # Limit task spawning rate
    )
    for greeting in greetings_ordered:
        print(greeting)

    print("\nRunning tasks with aiometer.amap (unordered results as they become available):")
    async with aiometer.amap(get_greeting, names, max_at_once=2) as greetings_unordered:
        async for greeting in greetings_unordered:
            print(greeting)

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

view raw JSON →