Module 4: AsyncIO & Concurrency (The Busy Chef)
📚 Module 4: AsyncIO & Concurrency
Course ID: PY-104
Subject: The Busy Chef
Python is normally Synchronous. This means it does one thing at a time. But in Data Engineering, we often spend a lot of time “Waiting”—waiting for a file to download or waiting for a database to answer. We use AsyncIO to stop wasting that time.
🏗️ Step 1: The “Waiting” Problem
Imagine you are a Chef in a kitchen.
- Synchronous: You put the toast in the toaster. You stand there and watch it. You don’t do anything else. When the toast is done, you start the eggs. This is very slow!
🏗️ Step 2: AsyncIO (The “Multitasking Chef”)
AsyncIO is the art of doing other things while you wait for a task to finish.
🧩 The Analogy: The Professional Kitchen
Imagine you are a professional Chef.
- Start the Toast: You put the bread in and say: “Hey toaster, tell me when you’re done.”
- Start the Eggs: While the toast is cooking, you start frying the eggs.
- Finish the Coffee: While the eggs are frying, you start the coffee machine.
- Completion: The coffee machine bleeps! You pour the coffee. The toaster pops! You grab the toast. The eggs are done! You plate everything up.
This is AsyncIO! You didn’t actually “cook everything at the same time” (Multiprocessing). You just multitasked by not standing around and waiting for any one task to finish.
🏗️ Step 3: Await & Async (The “New Vocabulary”)
To do this in Python, we use two new words:
- async: Put this before a function name to say: “This function is a Multitasking Chef.”
- await: Put this inside the function to say: “I am going to wait here for this to finish, but please go work on other things while I wait.”
🧪 Step 4: Python Practice (Simulating Multitasking)
Run this code to see how we can “Download” two things at the same time.
import asyncio
async def download_file(name, time_to_wait):
print(f"Starting download of {name}...")
# Simulate a slow download
await asyncio.sleep(time_to_wait)
print(f"Finished download of {name}!")
async def main():
print("Main Started!")
# Start both downloads at the same time
await asyncio.gather(
download_file("File 1", 2),
download_file("File 2", 1)
)
print("Main Finished!")
# Run the multitasker
asyncio.run(main())🥅 Module 4 Review
- Synchronous: Doing one thing at a time (One-by-One).
- Asynchronous: Multitasking while waiting for I/O tasks (Waiting while working).
- Await: Pausing a task while the computer works on something else.
- Concurrency: The ability to handle many tasks at once without needing a giant computer.
:::tip Slow Learner Note AsyncIO is only for Waiting (like network or disk). If you are doing Heavy Math (CPU work), you still need Multiprocessing or NumPy! :::