Advertisement
DigitalMag

asyncio hello world

Nov 11th, 2020 (edited)
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. import asyncio
  2. import time
  3.  
  4. loop = asyncio.get_event_loop()
  5.  
  6. async def waiter():
  7.     task1 = loop.create_task(cook('Pasta', 8))
  8.     task2 = loop.create_task(cook('Cesar', 3))
  9.     task3 = loop.create_task(cook('Chops', 16))
  10.  
  11.     await task1
  12.     await task2
  13.     await task3
  14.  
  15. async def cook(order, time_to_prepare):
  16.     print(f'New order: {order}')
  17.     await asyncio.sleep(time_to_prepare)
  18.     print(order, '- done')
  19.  
  20. loop.run_until_complete(waiter())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement