Limit Concurrency in AsyncIO
You can run multiple async tasks simultaneously in Python using the asyncio native library. This allows you to create and manage asynchronous tasks, such as coroutines, and run them concurrently. Let’s have the following async method, that counts to the length of the given name and returns the name. import asyncio from typing import Awaitable async def count_word(name: str) -> Awaitable[str]: if len(name) > 8: raise ValueError(f"{name} is too long...") for ii in range(len(name)): print(name, ii) await asyncio.sleep(1) return name Now, running this task twice: await count_word("first"), await count_word("second") first 0 first 1 first 2 first 3 first 4 second 0 second 1 second 2 second 3 second 4 second 5 ('first', 'second') Will not run the two tasks concurrently because it’s using await, which means that it will wait for the first task to complete before starting the second task. If you want to run these tasks concurrently, you should use asyncio.gather(): ...