Python course Β· Module 6: Async and FastAPI
Asynchronicity - parallel observations
In this lesson12
Welcome to Module 6! Darwin here with an exciting topic - asynchronous programming in Python!
Up until now, all your programs have been synchronous - executing operations one after another. Now you'll learn about asynchronicity - a technique that allows you to perform many tasks simultaneously!
Safari Analogy: Synchronous programming is like one guide observing animals one by one - first lions (waits 30 minutes), then elephants (waits 20 minutes), then cheetahs (waits 15 minutes). Asynchronous is like many guides observing simultaneously - one at the lions, another at the elephants, a third at the cheetahs - all in parallel! β±
The problem with synchronous code
Synchronous code - waiting in sequence
import time
def observe_lions():
"""Observing lions - takes 3 seconds"""
print("Starting lion observation...")
time.sleep(3) # Simulating wait
print("Lion observation complete!")
return {"species": "Lion", "count": 5}
def observe_elephants():
"""Observing elephants - takes 2 seconds"""
print("Starting elephant observation...")
time.sleep(2)
print("Elephant observation complete!")
return {"species": "Elephant", "count": 3}
def observe_cheetahs():
"""Observing cheetahs - takes 4 seconds"""
print("Starting cheetah observation...")
time.sleep(4)
print("Cheetah observation complete!")
return {"species": "Cheetah", "count": 2}
# Synchronous execution - ONE BY ONE
start = time.time()
result1 = observe_lions() # Waits 3s
result2 = observe_elephants() # Then waits 2s
result3 = observe_cheetahs() # Then waits 4s
end = time.time()
print(f"\nTotal time: {end - start:.1f}s")
# Output: Total time: 9.0s (3 + 2 + 4)Problem: Time = 9 seconds! We have to wait for each observation in sequence, even though we could observe in parallel! β°
The solution - asynchronous code
Asynchronous code - parallel waiting
import asyncio
async def observe_lions():
"""Asynchronous lion observation"""
print("Starting lion observation...")
await asyncio.sleep(3) # await instead of time.sleep!
print("Lion observation complete!")
return {"species": "Lion", "count": 5}
async def observe_elephants():
"""Asynchronous elephant observation"""
print("Starting elephant observation...")
await asyncio.sleep(2)
print("Elephant observation complete!")
return {"species": "Elephant", "count": 3}
async def observe_cheetahs():
"""Asynchronous cheetah observation"""
print("Starting cheetah observation...")
await asyncio.sleep(4)
print("Cheetah observation complete!")
return {"species": "Cheetah", "count": 2}
async def main():
"""Main async function"""
start = time.time()
# Execute in parallel!
results = await asyncio.gather(
observe_lions(),
observe_elephants(),
observe_cheetahs()
)
end = time.time()
print(f"\nTotal time: {end - start:.1f}s")
# Output: Total time: 4.0s (max of 3, 2, 4)
print(f"Results: {results}")
# Run async main
asyncio.run(main())Result: Time = 4 seconds! All observations execute in parallel!
Savings: 9s β 4s = 5 seconds faster (55% reduction)!
Async/await - syntax
async def - defining an asynchronous function
# Synchronous function
def sync_function():
return "Sync result"
# Asynchronous function (coroutine)
async def async_function():
return "Async result"async def creates a coroutine - a special function that can be "suspended" (await) and resumed later.
await - waiting for a result
async def fetch_species_data(species_id):
print(f"Fetching data for species {species_id}...")
await asyncio.sleep(2) # Simulating I/O operation
return {"id": species_id, "name": "Lion", "population": 120}
async def main():
# await suspends execution until a result is received
data = await fetch_species_data(1)
print(f"Received data: {data}")
asyncio.run(main())await says: "Wait for the result, but in the meantime let other tasks run".
IMPORTANT: await can only be used inside async def!
Event Loop - the async engine
The event loop is the heart of asynchronicity - it manages all coroutines and switches between them.
Analogy: The event loop is like a safari dispatcher - it assigns guides to different tasks, switches between them, and collects results!
import asyncio
async def task1():
print("Task 1 start")
await asyncio.sleep(1)
print("Task 1 end")
async def task2():
print("Task 2 start")
await asyncio.sleep(0.5)
print("Task 2 end")
async def main():
# Event loop manages these tasks
await asyncio.gather(task1(), task2())
# asyncio.run() creates the event loop and executes main()
asyncio.run(main())Output:
Task 1 start
Task 2 start
Task 2 end (after 0.5s)
Task 1 end (after 1s)The event loop was switching between task1 and task2!
asyncio.gather() - parallel execution
asyncio.gather() executes multiple coroutines in parallel and returns a list of results:
import asyncio
async def get_species(species_id):
await asyncio.sleep(1)
return {"id": species_id, "name": f"Species {species_id}"}
async def main():
# Fetch 5 species in parallel
results = await asyncio.gather(
get_species(1),
get_species(2),
get_species(3),
get_species(4),
get_species(5)
)
print(f"Fetched {len(results)} species:")
for species in results:
print(f" - {species['name']}")
asyncio.run(main())Time: 1 second (instead of 5 seconds synchronously)!
gather() returns results in the same order as you provided the coroutines!
asyncio.create_task() - run in the background
create_task() starts a coroutine in the background (does not wait for the result):
async def background_observation(species):
print(f"Starting observation of {species}...")
await asyncio.sleep(3)
print(f"Observation of {species} complete!")
async def main():
# Run in the background
task1 = asyncio.create_task(background_observation("Lion"))
task2 = asyncio.create_task(background_observation("Elephant"))
print("Doing other things in the meantime...")
await asyncio.sleep(1)
print("Still doing other things...")
# Wait for tasks to complete
await task1
await task2
asyncio.run(main())Output:
Starting observation of Lion...
Starting observation of Elephant...
Doing other things in the meantime...
Still doing other things...
Observation of Lion complete!
Observation of Elephant complete!Async comprehensions
Python allows async list/dict comprehensions:
async def get_population(species_id):
await asyncio.sleep(0.1)
return species_id * 10
async def main():
# Async list comprehension
populations = [await get_population(i) for i in range(1, 6)]
print(f"Populations: {populations}")
# Output: Populations: [10, 20, 30, 40, 50]
# But this executes SEQUENTIALLY!
# Better to use gather():
populations = await asyncio.gather(
*[get_population(i) for i in range(1, 6)]
)
print(f"Populations (parallel): {populations}")
asyncio.run(main())Practical example - Safari API Client
An asynchronous Safari API client:
import asyncio
import aiohttp # pip install aiohttp
class SafariAPIClient:
def __init__(self, base_url: str):
self.base_url = base_url
async def get_species(self, species_id: int):
"""Fetch species data"""
async with aiohttp.ClientSession() as session:
async with session.get(f"{self.base_url}/species/{species_id}") as response:
return await response.json()
async def get_multiple_species(self, species_ids: list[int]):
"""Fetch multiple species in parallel"""
async with aiohttp.ClientSession() as session:
tasks = []
for species_id in species_ids:
task = session.get(f"{self.base_url}/species/{species_id}")
tasks.append(task)
responses = await asyncio.gather(*tasks)
results = []
for response in responses:
data = await response.json()
results.append(data)
return results
async def main():
client = SafariAPIClient("https://api.safari-db.com")
# Fetch 10 species in parallel
species_ids = list(range(1, 11))
start = time.time()
results = await client.get_multiple_species(species_ids)
end = time.time()
print(f"Fetched {len(results)} species in {end - start:.2f}s")
asyncio.run(main())Synchronously: 10 requests Γ 0.5s = 5 seconds Asynchronously: max(0.5s) = 0.5 seconds
When to use async?
Use async when:
- I/O operations - HTTP requests, databases, files
- Many parallel operations - 100+ API requests
- WebSockets - real-time communication
- Waiting - sleep, timers, long-running computations
Do NOT use async when:
- CPU-bound - mathematical computations, image processing
- Simple code - small application without I/O
- No async libraries - the library doesn't support async
For CPU-bound use multiprocessing instead of async!
Async context managers
class AsyncDatabaseConnection:
async def __aenter__(self):
print("Connecting to database...")
await asyncio.sleep(1)
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
print("Closing connection...")
await asyncio.sleep(0.5)
async def query(self, sql):
print(f"Executing: {sql}")
await asyncio.sleep(0.2)
return [{"id": 1, "name": "Lion"}]
async def main():
async with AsyncDatabaseConnection() as db:
results = await db.query("SELECT * FROM species")
print(f"Results: {results}")
asyncio.run(main())Error handling in async
async def risky_operation(species_id):
if species_id == 3:
raise ValueError(f"Species {species_id} does not exist!")
await asyncio.sleep(1)
return {"id": species_id}
async def main():
try:
results = await asyncio.gather(
risky_operation(1),
risky_operation(2),
risky_operation(3), # Error!
return_exceptions=True # Return exceptions instead of raising
)
for i, result in enumerate(results, 1):
if isinstance(result, Exception):
print(f"Species {i}: ERROR - {result}")
else:
print(f"Species {i}: OK - {result}")
except Exception as e:
print(f"Error: {e}")
asyncio.run(main())Output:
Species 1: OK - {'id': 1}
Species 2: OK - {'id': 2}
Species 3: ERROR - Species 3 does not exist!Summary
In this lesson you learned:
- Sync vs Async - differences and benefits
async defandawait- syntax- Event loop - the engine of asynchronicity
asyncio.gather()- parallel executionasyncio.create_task()- background tasks- Async comprehensions
- Practical Safari API examples
- When to use async (I/O) vs multiprocessing (CPU)
- Async context managers and error handling
Final Safari Analogy: Async is like many safari guides observing different species simultaneously - instead of waiting 9 seconds observing one by one (sync), they observe in parallel and finish in 4 seconds (async)! The event loop is the dispatcher coordinating all the guides! β±
Next lesson: Darwin will show you FastAPI - a modern async framework for building blazing-fast APIs!
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is asynchronous programming?
2. How do you define an asynchronous function in Python?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Vertical ordering
Arrange a simple asynchronous function in Python:
- Click in order
Arrange the asyncio.gather call to run multiple coroutines:
- Code editor
Write an async function observe_animal that waits 1 second (asyncio.sleep) and returns the animal's name.