Created
June 27, 2020 20:23
-
-
Save nicksspirit/14b1173c628e9da419bee7ac4a2a54ff to your computer and use it in GitHub Desktop.
Start many tasks concurrently but with a limit of how many tasks can run at a time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| async def gather_with_concurrency(n, *tasks): | |
| semaphore = asyncio.Semaphore(n) | |
| async def concurrent_task(task): | |
| async with semaphore: | |
| await task | |
| await asyncio.gather(*(concurrent_task(task) for task in tasks)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment