SHARE
TWEET

Untitled

a guest Oct 17th, 2019 81 in 332 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Create async tasks and observe overall execution time
  2.  
  3. import time
  4. import asyncio
  5.  
  6. async def eggs(t):
  7.     start = time.time()
  8.     await asyncio.sleep(t)
  9.     elapsed = time.time() - start
  10.     print(f'Eggs done {elapsed:.1f}s')
  11.  
  12. async def toast(t):
  13.     start = time.time()
  14.     await asyncio.sleep(t)
  15.     elapsed = time.time() - start
  16.     print(f'Toast done {elapsed:.1f}s')
  17.  
  18. async def main():
  19.     start = time.time()
  20.     eggs_task = asyncio.create_task(eggs(2))
  21.     toast_task = asyncio.create_task(toast(3))
  22.     await eggs_task
  23.     await toast_task
  24.     elapsed = time.time() - start
  25.     print(f'Time for breakfast {elapsed:.1f}s')
  26.  
  27. asyncio.run(main())
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top