Skip to main content

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.

#LessonFocusSmartNotes Deliverable
1Why Concurrency?Blocking I/O, concurrency vs parallelism, GIL conceptTimed sequential export baseline
2Threading BasicsThread, start/join, I/O-bound thread benefitThreaded export timing comparison
3Your First Coroutineasync def, await, asyncio.run, asyncio.sleepasync export_note with simulated delay
4Running Tasks Concurrentlyasyncio.gather, create_task, concurrent timingasync_export_all using gather
5Async Iteration and Context Managersasync for, async with, aiofilesAsync file reading with aiofiles
6Testing Async Codepytest-asyncio, @pytest.mark.asyncioTests for async export/import
7SmartNotes Async CapstoneFull TDG: async export/import pipelineComplete 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).