Created
January 15, 2019 00:28
-
-
Save njsmith/e42a986723bc77e16d764ca096bd031d to your computer and use it in GitHub Desktop.
This file contains 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
# 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