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

2.6 KiB
Raw Permalink Blame History

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)

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
  1. 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
  1. 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
  1. 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