What is the maximum number of coroutines that can be launched simultaneously in an application?
sobes.tech AI
Answer from AI
Python does not have a strict limit on the maximum number of concurrently running coroutines. A coroutine can be thought of as a lightweight task managed by the asyncio scheduler.
The limitations depend on system resources (RAM, CPU time) and how the coroutines utilize these resources (for example, if they are actively waiting for I/O, there can be many of them).
In practice, you can run thousands or even tens of thousands of coroutines simultaneously if they do not block the thread and efficiently use asynchronous operations.
Example of launching multiple coroutines:
import asyncio
async def worker(n):
await asyncio.sleep(1)
print(f"Worker {n} done")
async def main():
tasks = [asyncio.create_task(worker(i)) for i in range(10000)]
await asyncio.gather(*tasks)
asyncio.run(main())
Here, 10,000 coroutines are launched, and this usually works without issues if there is enough memory and no blocking operations.
Thus, the maximum number of coroutines is mainly limited by system resources and implementation specifics, not by Python itself.