Chapter 66 — Async Python
The SmartNotes CLI works. But when James runs smartnotes export --format all, the tool writes Markdown files, then JSON, then CSV. Each format waits for the previous one to finish. One hundred notes across three formats takes seconds of pure waiting. The program is not slow because Python is slow. It is slow because it does one thing at a time while the disk sits idle.
This chapter teaches async/await: Python's mechanism for doing multiple I/O operations without waiting. By the end, SmartNotes exports files concurrently and you understand the pattern that powers every AI SDK, every web API, and every MCP server you will encounter in Part 6.
| # | Lesson | Focus | SmartNotes Deliverable |
|---|---|---|---|
| 1 | Why Concurrency? | Blocking I/O, concurrency vs parallelism, GIL concept | Timed sequential export baseline |
| 2 | Threading Basics | Thread, start/join, I/O-bound thread benefit | Threaded export timing comparison |
| 3 | Your First Coroutine | async def, await, asyncio.run, asyncio.sleep | async export_note with simulated delay |
| 4 | Running Tasks Concurrently | asyncio.gather, create_task, concurrent timing | async_export_all using gather |
| 5 | Async Iteration and Context Managers | async for, async with, aiofiles | Async file reading with aiofiles |
| 6 | Testing Async Code | pytest-asyncio, @pytest.mark.asyncio | Tests for async export/import |
| 7 | SmartNotes Async Capstone | Full TDG: async export/import pipeline | Complete async pipeline with timing |
Prerequisites: SmartNotes CLI (Ch 65), file I/O (Ch 62), decorators concept (Phase 5 Ch 61 for understanding @pytest.mark.asyncio), TDG mastery (Ch 57).