Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try:
- import os
- import sys
- import asyncio
- print("{}".format(sys.version))
- print("\n")
- except Exception as e:
- print("Some modules are missing :{} ".format(e))
- class Compute(object):
- def __init__(self, num):
- self.num = num
- async def square(self):
- print("Computing Square : {} ".format(self.num))
- await asyncio.sleep(3)
- print("Square of Number is : {} ".format(self.num))
- return self.num * self.num
- async def cube(self):
- print("Computing cube : {} ".format(self.num))
- await asyncio.sleep(3)
- # you have just defined a task
- task = asyncio.create_task(coro=self.square())
- res = await task
- num = res * self.num
- print("Cube : {} ".format(num))
- return num
- async def main():
- _instance = Compute(2)
- task1 = asyncio.create_task(coro=_instance.square())
- task2 = asyncio.create_task(coro=_instance.cube())
- await task1
- await task2
- if __name__ == "__main__":
- asyncio.run(main())
- # _instance =Compute(2)
- # asyncio.run(_instance.cube())
Advertisement
Add Comment
Please, Sign In to add comment