Skip to content

Instantly share code, notes, and snippets.

@zema1
Created December 29, 2019 11:16
Show Gist options
  • Save zema1/02fa0c4446c050f6a3ed715a9b245343 to your computer and use it in GitHub Desktop.
Save zema1/02fa0c4446c050f6a3ed715a9b245343 to your computer and use it in GitHub Desktop.
import asyncio
import functools
import random
from multiprocessing import Manager
from aiomultiprocess import Pool
from aiomultiprocess.types import Queue
async def work(q: Queue, sleep_time: int):
await asyncio.sleep(sleep_time)
q.put(1)
async def main():
m = Manager()
q = m.Queue()
wrapped_work = functools.partial(work, q)
tasks = [random.randint(1, 5) for _ in range(10)]
print("sleep time", tasks)
asyncio.get_event_loop().run_in_executor(None, stats, q, len(tasks))
async with Pool() as pool:
await pool.map(wrapped_work, tasks)
def stats(q, total):
done = 0
while True:
q.get()
done += 1
print(f"done/total: {done}/{total}")
if done == total:
return
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment