Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import random
- from concurrent.futures import as_completed,ProcessPoolExecutor
- import time
- async def thr(i):
- while True:
- try:
- loop = asyncio.new_event_loop()
- asyncio.set_event_loop(loop)
- tasks=[do_stuff(i,"A"),do_stuff(i,"B"),do_stuff(i,"C"),do_stuff(i,"D")]
- ret = loop.run_until_complete(await asyncio.gather(*tasks, return_exceptions=False))
- loop.close()
- return ret
- except Exception as e:
- print(e)
- async def do_stuff(i,char):
- while True:
- try:
- ran = random.uniform(0.1, 1)
- time.sleep(ran)
- if char == "A":
- time.sleep(1)
- print(i,char, ran)
- return ran
- except Exception as e:
- print(e)
- def main():
- futures = []
- retDict = []
- with ProcessPoolExecutor(max_workers=6) as executor:
- for i in range(0,5,1):
- # print(i)
- future = executor.submit(thr, i)
- futures.append(future)
- for future in as_completed(futures):
- retDict+=future.result()
- print("im here")
- # print(retDict)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement