Python in Practice
Master the standard library and tackle concurrency — asyncio, threading, and multiprocessing.
`os` and `sys` — System Interface
`os` provides a portable interface to operating system services: file/directory ...
`pathlib` — Object-Oriented File Paths
`pathlib.Path` (Python 3.4+) replaces string-based path manipulation with an obj...
`json` — JSON Encoding and Decoding
`json.dumps()` serializes Python objects to JSON strings; `json.loads()` parses ...
`re` — Regular Expressions
The `re` module provides Perl-compatible regular expressions. Key functions: `re...
`datetime` — Dates, Times, and Timezones
The `datetime` module provides `date`, `time`, `datetime`, `timedelta`, and `tim...
`collections` — High-Performance Containers
The `collections` module provides specialized container types. `Counter` counts ...
`itertools` — Iterator Building Blocks
`itertools` provides memory-efficient combinatoric and functional iterators. Inf...
`functools` — Higher-Order Functions
`functools` provides tools for higher-order functions. `lru_cache` (and `cache`)...
`contextlib` — Context Manager Utilities
`contextlib` provides utilities for creating and composing context managers. `@c...
`logging` — Structured Logging
The `logging` module provides a configurable, hierarchical logging system. Logge...
`unittest` — Testing Framework
`unittest` is Python's built-in xUnit-style testing framework. Test cases extend...
`subprocess` — Running External Commands
`subprocess.run()` (Python 3.5+) is the recommended way to run shell commands. `...
`argparse` — Command-Line Argument Parsing
`argparse` builds fully-featured CLI parsers from Python code. It auto-generates...
`typing` — Advanced Type Annotations
The `typing` module provides building blocks for complex type annotations. `Type...
The Global Interpreter Lock (GIL)
The GIL is a mutex in CPython that allows only one thread to execute Python byte...
`threading` — OS-Level Threads
`threading.Thread` runs a callable in an OS thread. Threads share the process's ...
`multiprocessing` — True Parallelism
`multiprocessing.Process` launches a separate Python interpreter process — no GI...
`concurrent.futures` — High-Level Executor Interface
`concurrent.futures` provides `ThreadPoolExecutor` (threads, good for I/O) and `...
`asyncio` — Cooperative Concurrency
`asyncio` runs many coroutines on a single thread using cooperative multitasking...
Coroutines and `async`/`await`
A coroutine is a function defined with `async def` that returns a coroutine obje...
`asyncio.Queue` — Async Producer-Consumer
`asyncio.Queue` is a thread-safe, async-friendly queue for producer-consumer pat...
Locks, Events, and Semaphores
`threading.Lock` is a mutual exclusion lock — only one thread can hold it at a t...
Shared State and Thread Safety
Sharing mutable state between threads without synchronization causes race condit...
Performance Patterns — Choosing the Right Concurrency Model
Python offers three concurrency models: `threading` (I/O-bound, shared memory, G...