Files
career-progression-2026/week-01-python-internals/day-002-asyncio/week-01-python-internals_day-02-asyncio_notes.md
2026-03-14 16:37:03 -05:00

61 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### Day 2 — asyncio fundamentals (event loop, tasks, gather)
#### Goals (what "done" means)
- [ ] I can explain the event loop + coroutines + tasks in plain language.
- [ ] I can write a small asyncio program using `create_task` + `gather`.
- [ ] I can add timeouts, cancellation handling, and concurrency limits.
#### Reading (official first)
- [ ] Read (core usage): [Coroutines and Tasks — Python docs](https://docs.python.org/3/library/asyncio-task.html)
- [ ] Note what each does: `asyncio.run`, `asyncio.create_task`, `asyncio.gather`, cancellation.
- [ ] Read (practical walkthrough): [Pythons asyncio: A Hands-On Walkthrough (Real Python)](https://realpython.com/async-io-python/)
- [ ] Write 5 bullets: when asyncio is a good fit, and when its not.
- [ ] Read (debugging): [Developing with asyncio — Python docs](https://docs.python.org/3/library/asyncio-dev.html#debug-mode)
- [ ] Note 2 debugging tips you can reuse later.
#### Hands-on (build incrementally)
1. **Minimal concurrency demo (no external libs)**
- [ ] Write `async def worker(i):` that does `await asyncio.sleep(...)` and returns a value
- [ ] Run N workers concurrently with `asyncio.gather`
- [ ] Print start/end timestamps to show concurrency
2. **Add concurrency limit**
- [ ] Add `asyncio.Semaphore(k)` and wrap the worker body so only `k` run at once
- [ ] Demonstrate by running N=50 with k=5 and showing batching behavior
3. **Add timeouts + error isolation**
- [ ] Wrap each worker with `asyncio.wait_for(..., timeout=...)`
- [ ] Make some workers intentionally exceed timeout
- [ ] Use `gather(..., return_exceptions=True)` and summarize:
- [ ] successes
- [ ] timeouts
- [ ] other exceptions
4. **Cancellation behavior**
- [ ] Trigger cancellation of a running task and confirm you handle `asyncio.CancelledError` cleanly
- [ ] Write 3 bullets: what cancellation means and how to design for it
#### Write-up (in notes)
- [ ] "Mental model" section (810 bullets):
- [ ] coroutine vs task
- [ ] scheduling
- [ ] awaiting vs creating tasks
- [ ] why blocking calls break concurrency
- [ ] "Pitfalls" section (at least 5 bullets):
- [ ] blocking I/O
- [ ] CPU-bound work in asyncio
- [ ] forgetting to await
- [ ] unhandled exceptions in tasks
- [ ] cancellation edge cases
#### Deliverables (to commit)
- [ ] `notes.md` updated with:
- [ ] mental model bullets
- [ ] code snippets or links to your local scripts
- [ ] brief results/observations
- [ ] `asyncio_demo.py` (or similar) added with instructions to run
#### Suggested commits
- [ ] `day02: add asyncio notes + minimal gather demo`
- [ ] `day02: add semaphore, timeout, and cancellation examples`