Standard library & async
Choose batteries-included Python tools and design correct concurrent programs.
Python's standard library covers files, dates, URLs, containers, iteration, mathematics, and concurrency. Learning it is less about memorizing every function and more about choosing the right abstraction and understanding its contracts.
Learning goals
By the end of this section, you will be able to:
- navigate the operating system safely with
pathlib,os, andsys; - represent instants correctly with timezone-aware
datetimevalues; - select numerical, statistical, HTTP, container, and iterator APIs;
- write cooperative asynchronous programs with
asyncio; - use structured concurrency, cancellation, timeouts, and synchronization correctly;
- choose among threads, processes, and async tasks based on the workload.
A practical selection guide
| Need | Start with | Why |
|---|---|---|
| Paths and routine file I/O | pathlib.Path | Composable, readable, cross-platform path objects |
| Environment, processes, low-level OS details | os | Direct operating-system interfaces |
| Interpreter arguments and runtime | sys | Python process state, streams, and exit status |
| Instants and calendar arithmetic | datetime + zoneinfo | Aware timestamps and IANA timezone rules |
| Accurate decimal money | decimal.Decimal | Base-10 arithmetic with explicit rounding |
| Descriptive statistics | statistics | Clear standard formulas for ordinary datasets |
| Simple HTTP client calls | urllib.request | Dependency-free, synchronous HTTP |
| Counting/grouping/default values | collections | Purpose-built containers |
| Lazy pipelines and combinatorics | itertools | Memory-efficient iterator building blocks |
| Many waiting network operations | asyncio | Cooperative concurrency on one event loop |
| Blocking I/O with synchronous libraries | Threads | Straightforward overlap of blocking waits |
| CPU-heavy Python work | Processes | Multiple interpreters avoid the GIL bottleneck |
The concurrency decision
Concurrency is not automatically parallelism:
- Async tasks share one thread and yield at
await. They are excellent when the whole call chain can be async and most time is spent waiting. - Threads share memory and are useful for blocking I/O or libraries without async APIs. Shared mutable state may require thread locks.
- Processes have separate memory and can execute CPU-bound Python in parallel. Data must be serialized between processes, so startup and communication cost matter.
Synchronization is needed whenever concurrent workers can interleave access to an invariant.
It is not limited to threads: async tasks can race whenever a logical read-modify-write operation
contains an await.
How to study this section
Run examples on Python 3.12 or newer. For each API:
- identify the input and output types;
- note ownership and lifetime (especially files, tasks, and executors);
- test failure paths, cancellation, and empty inputs;
- prefer context managers and structured scopes;
- consult the reference page before reaching for a third-party dependency.
Warm-up practice
Design a command-line program that accepts a directory, finds .txt files, reports word counts,
and optionally fetches a remote stop-word list. Decide which parts need sys, pathlib,
collections, urllib, or asyncio. Explain whether concurrency would improve it and what
resource limit you would impose.
LEARNING RECORD
Finish this lesson
Mark it complete when you can explain the main decision without looking.
KNOWLEDGE CHECK