High-Performance Python with Rust
Class Duration
14 hours of live training delivered over 2-4 days to accommodate your scheduling needs.
Student Prerequisites
- Significant working experience in both Python and Rust
- Comfort with packaging, virtual environments, and the Rust toolchain
- This course is not a Rust introduction; for that, take Rust for Python Programmers first
Target Audience
Python and Rust developers building native Python extensions for performance, ML, data, or systems work. Particularly relevant for ML/data engineers shipping LLM applications who need to drop CPython hot paths into Rust without losing the productivity of Python at the boundaries, and for teams maintaining mature Python codebases that need a faster, GIL-free runtime for specific components.
Description
This advanced course is for engineers who already know Python and Rust and want to ship a real, debuggable, GIL-free native Python extension. It covers the full workflow on the Rust 2024 edition (Rust 1.85+) and PyO3 0.28+: profiling Python to find the right hot path, building Rust extensions with PyO3 + Maturin, exposing functions and classes across the boundary, releasing the GIL for CPU-bound work, threading and async/await across the boundary, and packaging as wheels for PyPI. AI assistants accelerate the routine parts (boilerplate, type conversions, error mapping); the course teaches where they help and where you still have to think carefully about ownership, the GIL, and what crosses the FFI boundary.
Learning Outcomes
- Profile Python applications to identify the hot paths actually worth rewriting in Rust.
- Build Rust extensions with PyO3 0.28+ and Maturin from scratch, including modules, classes, functions, and exceptions.
- Convert types across the Python/Rust boundary correctly and understand the costs.
- Release the GIL for CPU-bound Rust work and benchmark the speedup honestly.
- Use threading, parallelism, and async/await across the Python/Rust boundary.
- Package and publish Python wheels to PyPI with a sensible CI build matrix.
- Use AI coding assistants productively for PyO3 work without letting them obscure GIL-handling or unsoundness.
Training Materials
All students receive comprehensive courseware covering all topics in the course. Courseware is distributed via GitHub in the form of documentation and extensive code samples. Students practice the topics covered through challenging hands-on lab exercises and a capstone in which a Python hot path is rewritten in Rust, parallelized off-GIL, and shipped as a wheel.
Software Requirements
A free GitHub account, the latest stable Rust toolchain installed via rustup (Rust 1.95+ on the 2024 edition), Python 3.12+, Visual Studio Code or another supported editor with the rust-analyzer extension, Maturin, and an AI coding assistant of choice (GitHub Copilot, Cursor, or Claude Code). A cloud-based environment can be provided if local installation is restricted.
Training Topics
Why Rust Inside Python
- The shape of Python performance problems in 2026
- CPU-bound vs. IO-bound: choosing the right tool
- Where PyO3 wins and where to stay in Python
- Cost model for native extensions: build, deploy, maintain
Profiling and Measurement
- Profiling Python with
py-spy,scalene, andcProfile - Detecting reference counting and memory leaks
- Building a debug Python and using Valgrind where it actually helps
- Benchmarking Python and Rust together with
pytest-benchmarkandcriterion
Debugging Across the Boundary
- Compiling Rust extensions with debug symbols
- Debugging Rust + Python in VS Code
- Symbol management on Linux, macOS, and Windows
- Reproducing crashes in CI
PyO3 and Maturin Workflow
- PyO3 0.28+ vs. earlier APIs: what changed and why
- Project layout with Maturin
- The
pyo3macros:#[pymodule],#[pyfunction],#[pyclass],#[pymethods] - Building, installing, and reloading during development
- Publishing wheels with
maturin publish
Python and Rust Integration
- Modules and submodules in Rust, including declarative modules
- Functions, signatures, and default arguments
- Type conversions:
&PyAny,Py<T>,PyResult - Exposing Rust classes to Python with
#[pyclass] - Mapping Rust errors to Python exceptions
- Calling Python functions from Rust
The GIL and Parallelism
- The CPython GIL: what it does and what it costs
- Releasing the GIL with
Python::allow_threads - Threading and
Send/Syncboundaries - Parallelism with
rayoninside Rust extensions - Async/await across the boundary:
pyo3-asyncioand friends - Honest measurement of GIL-free speedups
Distributing Rust Extensions
- Production builds and optimization profiles
- Building wheels with
maturinfor Linux, macOS, and Windows - Building manylinux wheels with
cibuildwheel - Publishing to PyPI and an internal index
- Versioning and ABI considerations
AI-Assisted PyO3 Workflow
- Where AI assistants help on PyO3 boilerplate and where they get the GIL or ownership wrong
- Prompts that work well for type conversions and error mapping
- Letting the compiler, clippy, and tests be the source of truth
- Reviewing AI-generated
unsafe,unwrap, andPython::allow_threadsblocks - An AI-assisted refactor lab on a Python hot path
Capstone
- Pick a CPU-bound Python hot path, rewrite it in Rust as a PyO3 extension, release the GIL, parallelize with Rayon, ship as a wheel, and demonstrate the speedup with
pytest-benchmark— built with AI assistance and validated by the compiler, clippy, and a benchmark - Course wrap-up and recommended next courses