Guest User

asyncio

a guest
May 25th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. try:
  2.     import os
  3.     import sys
  4.     import asyncio
  5.     from queue import Queue
  6.     print("{} ".format(sys.version))
  7. except Exception as e:
  8.     print("Some modules are missing : {} ".format(e))
  9.  
  10. square = Queue()
  11.  
  12.  
  13. async def compute_square(num):
  14.     print("computing square : {} ".format(num))
  15.     await asyncio.sleep(3)
  16.     print("Square of Number is : {} ".format(num*num))
  17.     square.put(num*num)
  18.  
  19.  
  20.  
  21. async def compute_cube(num):
  22.     print("computing cube : {} ".format(num))
  23.     await asyncio.sleep(3)
  24.     print("Square of Number is : {} ".format(num * num * num))
  25.     return num* num * num
  26.  
  27.  
  28. async def main():
  29.  
  30.     task1 = asyncio.create_task(coro=compute_square(33))
  31.     await task1
  32.  
  33.     while True:
  34.         flag = square.empty()
  35.         if flag:
  36.             pass
  37.         else:
  38.             response = square.get()
  39.             print(response)
  40.             break
  41.  
  42.     #task2  = asyncio.create_task(coro=compute_cube(33))
  43.  
  44.     # try:
  45.     #
  46.     #     await asyncio.wait_for(task1, timeout=1)
  47.     # except Exception as e:
  48.     #     pass
  49.  
  50.     # response = await task1
  51.     #await task2
  52.  
  53.  
  54. if __name__ == "__main__":
  55.     asyncio.run(main())
Add Comment
Please, Sign In to add comment