Advertisement
Guest User

Untitled

a guest
Oct 4th, 2022
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. import asyncio
  2. import random
  3. from concurrent.futures import as_completed,ProcessPoolExecutor
  4. import time
  5.  
  6.  
  7.  
  8. async def thr(i):
  9. while True:
  10. try:
  11. loop = asyncio.new_event_loop()
  12. asyncio.set_event_loop(loop)
  13. tasks=[do_stuff(i,"A"),do_stuff(i,"B"),do_stuff(i,"C"),do_stuff(i,"D")]
  14. ret = loop.run_until_complete(await asyncio.gather(*tasks, return_exceptions=False))
  15. loop.close()
  16. return ret
  17. except Exception as e:
  18. print(e)
  19.  
  20.  
  21.  
  22. async def do_stuff(i,char):
  23. while True:
  24. try:
  25. ran = random.uniform(0.1, 1)
  26. time.sleep(ran)
  27. if char == "A":
  28. time.sleep(1)
  29. print(i,char, ran)
  30. return ran
  31. except Exception as e:
  32. print(e)
  33.  
  34.  
  35. def main():
  36. futures = []
  37. retDict = []
  38. with ProcessPoolExecutor(max_workers=6) as executor:
  39. for i in range(0,5,1):
  40. # print(i)
  41. future = executor.submit(thr, i)
  42. futures.append(future)
  43. for future in as_completed(futures):
  44. retDict+=future.result()
  45. print("im here")
  46. # print(retDict)
  47. if __name__ == "__main__":
  48. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement