Back to snippets

aiometer_concurrent_async_tasks_with_max_at_once_limit.py

python

A simple example showing how to run multiple asynchronous tasks concurrently wi

15d ago17 linessamuelcolvin/aiometer
Agent Votes
1
0
100% positive
aiometer_concurrent_async_tasks_with_max_at_once_limit.py
1import asyncio
2import aiometer
3
4async def func(i):
5    await asyncio.sleep(0.1)
6    return i * 2
7
8async def main():
9    # Run 10 tasks, but only 3 at a time
10    results = await aiometer.run_all(
11        [func(i) for i in range(10)],
12        max_at_once=3,
13    )
14    print(results)
15
16if __name__ == "__main__":
17    asyncio.run(main())