Python Atlas
Standard library & async

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, and sys;
  • represent instants correctly with timezone-aware datetime values;
  • 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

NeedStart withWhy
Paths and routine file I/Opathlib.PathComposable, readable, cross-platform path objects
Environment, processes, low-level OS detailsosDirect operating-system interfaces
Interpreter arguments and runtimesysPython process state, streams, and exit status
Instants and calendar arithmeticdatetime + zoneinfoAware timestamps and IANA timezone rules
Accurate decimal moneydecimal.DecimalBase-10 arithmetic with explicit rounding
Descriptive statisticsstatisticsClear standard formulas for ordinary datasets
Simple HTTP client callsurllib.requestDependency-free, synchronous HTTP
Counting/grouping/default valuescollectionsPurpose-built containers
Lazy pipelines and combinatoricsitertoolsMemory-efficient iterator building blocks
Many waiting network operationsasyncioCooperative concurrency on one event loop
Blocking I/O with synchronous librariesThreadsStraightforward overlap of blocking waits
CPU-heavy Python workProcessesMultiple 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:

  1. identify the input and output types;
  2. note ownership and lifetime (especially files, tasks, and executors);
  3. test failure paths, cancellation, and empty inputs;
  4. prefer context managers and structured scopes;
  5. 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

Standard library and async

1A service must overlap calls to a synchronous database driver and also run CPU-heavy pure-Python analysis. Which design best matches the workloads?
2A CLI reads paths, counts repeated words, and records an elapsed timeout. Which standard-library combination uses the most appropriate abstractions?