Skip to content

Instantly share code, notes, and snippets.

@nicksspirit
Created June 27, 2020 20:23
Show Gist options
  • Select an option

  • Save nicksspirit/14b1173c628e9da419bee7ac4a2a54ff to your computer and use it in GitHub Desktop.

Select an option

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.
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