Created
February 18, 2023 00:09
-
-
Save brettvitaz/58c848423b88994319c2c25bde641628 to your computer and use it in GitHub Desktop.
Limit concurrent coroutines using async gather
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
async def gather_with_concurrency(n, *coros): | |
semaphore = asyncio.Semaphore(n) | |
async def sem_coro(coro): | |
async with semaphore: | |
return await coro | |
return await asyncio.gather(*(sem_coro(c) for c in coros)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment