Skip to content

Instantly share code, notes, and snippets.

@brettvitaz
Created February 18, 2023 00:09
Show Gist options
  • Save brettvitaz/58c848423b88994319c2c25bde641628 to your computer and use it in GitHub Desktop.
Save brettvitaz/58c848423b88994319c2c25bde641628 to your computer and use it in GitHub Desktop.
Limit concurrent coroutines using async gather
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