Last active
March 7, 2021 17:12
-
-
Save qenops/48e8882a7d7ecfa73e6a91e88cf3f150 to your computer and use it in GitHub Desktop.
Python asyncio tasks in two sets
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
import asyncio | |
from random import randrange | |
import time | |
async def mustDoTask(name): | |
count = 0 | |
start = time.time() | |
while count < 10: | |
print(f'longtask {name} loop {count}') | |
await asyncio.sleep(randrange(3,6)/10) | |
count += 1 | |
return time.time() - start | |
async def canDoTask(name): | |
count = 0 | |
while count < 10000000: | |
if count % 100000 == 0: | |
print(f'shorttask {name} loop {count}') | |
await asyncio.sleep(0) | |
count += 1 | |
def doTasks(mustDos, canDos, canDoInProgress=[]): | |
''' Asyncronously run two classifications of tasks: must do tasks and can do tasks | |
:param mustDos: list of coroutines that must be completed | |
:param canDos: list of coroutines that can be worked on | |
:param canDoInProgress: Optional; list of can do tasks already in progress | |
:return: tuple of (results dict - results from finished tasks, list of unfinished tasks) | |
''' | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
mustDoTasks = [loop.create_task(t) for t in mustDos] | |
for t in mustDoTasks: | |
t.set_name(f'mustDo{t.get_name()}') | |
canDoTasks = [loop.create_task(t) for t in canDos] | |
for t in canDoTasks: | |
t.set_name(f'canDo{t.get_name()}') | |
unfinished = mustDoTasks + canDoTasks + canDoInProgress | |
results = {} | |
while any(t in mustDoTasks for t in unfinished): | |
finished, unfinished = loop.run_until_complete(asyncio.wait(unfinished, return_when=asyncio.FIRST_COMPLETED)) | |
for t in finished: | |
results[t.get_name()] = t.result() | |
return results, unfinished |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment