Skip to content

Instantly share code, notes, and snippets.

@tarekziade
Last active November 30, 2022 01:49
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 tarekziade/7fe1b1463664a71c50b0f33804068295 to your computer and use it in GitHub Desktop.
Save tarekziade/7fe1b1463664a71c50b0f33804068295 to your computer and use it in GitHub Desktop.
Run corountines in parallel with a maximum concurrency
class ConcurrentRunner:
def __init__(self, max_concurrency=5):
self.max_concurrency = 5
self.tasks = []
async def put(self, coro):
"""Starts a coroutine if there are 4 or less already running ones.
"""
# blocks until there's room
while len(self.tasks) >= self.max_concurrency:
await asyncio.sleep(0)
fut = asyncio.create_task(coro())
self.tasks.append(fut)
fut.add_done_callback(self.tasks.remove)
return fut
async def wait(self):
"""Wait for remaining routines to finish
"""
await asyncio.gather(*self.tasks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment