Module 05 / Project 05 — Async Web Server¶
Home: README · Module: Async Python
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
- Async FastAPI endpoints
- Background tasks with
BackgroundTasks - Streaming responses with
StreamingResponse - Running async operations inside endpoints
Why this project exists¶
FastAPI is async by default. This project brings together everything from this module — async functions, concurrent operations, queues — into a real web server. You will see how async makes web servers handle many requests without blocking.
Run¶
cd projects/modules/05-async-python/05-async-web-server
pip install -r ../requirements.txt
python app.py
# Visit http://127.0.0.1:8000/docs to see the API
Expected output¶
Then visit /docs in your browser to interact with the API.
Alter it¶
- Add an endpoint that fetches data from 3 external APIs concurrently and returns combined results.
- Add a WebSocket endpoint that sends progress updates to the client.
- Add a
/statsendpoint that returns how many background tasks have completed.
Break it¶
- Use
time.sleep(10)in an endpoint instead ofasyncio.sleep(). Try hitting another endpoint while it sleeps. - Remove
asyncfrom an endpoint that usesawait. What error do you get? - Start a background task that raises an exception. What happens to the response?
Fix it¶
- Replace
time.sleep()withasyncio.sleep()so other requests are not blocked. - Add error handling in background tasks so failures are logged, not lost.
- Add a timeout to the slow endpoint using
asyncio.wait_for().
Explain it¶
- Why are FastAPI endpoints
async defby default? - What happens when you define an endpoint with
def(no async) in FastAPI? - How do background tasks differ from running something in the endpoint itself?
- What would you use for truly long-running tasks that outlive a request?
Mastery check¶
You can move on when you can: - write async FastAPI endpoints, - use BackgroundTasks for deferred work, - explain why blocking calls hurt async servers, - stream data to clients with StreamingResponse.
Related Concepts¶
Next¶
Go back to Module index or continue to Module 06 — Databases & ORM.