Last active
November 30, 2022 01:49
-
-
Save tarekziade/7fe1b1463664a71c50b0f33804068295 to your computer and use it in GitHub Desktop.
Run corountines in parallel with a maximum concurrency
This file contains hidden or 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
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