Created
March 17, 2022 03:31
-
-
Save KianYang-Lee/f9f6a7457959f74c3264eaaad3f504fb to your computer and use it in GitHub Desktop.
Simple program to mimic how to cook breakfast in a concurrent fashion using asyncio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
import asyncio | |
async def prep_breakfast(): | |
start_time = time() | |
async def boil_water(): | |
print(">>> Start to boil water...") | |
await asyncio.sleep(8) # Sleep for 10 secs to mimic water boiling situation | |
print(">>> Water is boiled!") | |
async def fry_egg(): | |
print(">>> Getting eggs from fridge...") | |
print(">>> Washing eggs...") | |
print(">>> Start to fry egg...") | |
await asyncio.sleep(10) # Sleep for 10 secs to mimic egg frying situation | |
print(">>> Egg is done!") | |
async def make_bread(): | |
print(">>> Grabbing bread...") | |
print(">>> Apply jam on bread...") | |
boil_water_task = asyncio.create_task(boil_water()) | |
fry_egg_task = asyncio.create_task(fry_egg()) | |
make_bread_task = asyncio.create_task(make_bread()) | |
print(">>> Pouring 3-in-1 coffee powder into cup...") | |
await boil_water_task | |
print(">>> Pouring boiled water into cup...") | |
await fry_egg_task | |
await make_bread_task | |
print(">>> Prep and serve...") | |
print(">>> Breakfast is ready! =)") | |
end_time = time() | |
print(f">>> It took me {end_time - start_time} seconds to make today's breakfast.") | |
if __name__ == "__main__": | |
asyncio.run(prep_breakfast()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment