Skip to content

Instantly share code, notes, and snippets.

@njsmith
Created January 15, 2019 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsmith/e42a986723bc77e16d764ca096bd031d to your computer and use it in GitHub Desktop.
Save njsmith/e42a986723bc77e16d764ca096bd031d to your computer and use it in GitHub Desktop.
# One of several ways to handle a lot of jobs with a limit on how many run at once
# Context: https://gitter.im/python-trio/general?at=5c3d2643c45b986d116009bc
async def limited_concurrency(job_handler, jobs, max_at_once):
limiter = trio.Semaphore(max_at_once)
async def task_fn(job):
await job_handler(job)
limiter.release()
async with trio.open_nursery() as nursery:
for job in jobs:
await limiter.acquire()
nursery.start_soon(task_fn, job)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment