Curriculum/pyo3-ffi
PyO3 FFI
runtime·L2 · idiom·stub
Replacesthe belief that 'write a C extension' is the only way to escape Python's GIL.
PyO3 is a Rust crate that exposes Rust functions to Python with zero data copy for native types and ergonomic conversion for the rest. The Rust function automatically releases the GIL on entry (unless it touches Python objects); the call is ~tens of ns of FFI overhead plus the function's own time. This is the path Polars takes — and the same path your hot kernels take when pandas / numpy / polars cannot express what you need.
Prerequisites
Unlocks
Bridges
- c-extension-boundarymodel to implementationPyO3 is `Py_BEGIN_ALLOW_THREADS` / `Py_END_ALLOW_THREADS` from the CPython C API, wrapped in Rust's type system. Same underlying mechanism (the GIL state guard); safer surface (compiler enforces that you do not touch PyObjects while the GIL is released).
- gil-release-around-ffishared mechanismEvery Python C extension that does CPU work outside the interpreter (numpy, polars, pandas, sqlite, pillow, lxml) drops the GIL on entry and re-acquires on return. PyO3 inherits this convention. The convention is what makes Stage 1's GIL escape hatches actually work.
This concept is a node in the curriculum DAG. The full lab — page blocks, done state, references — has not been authored yet. The relations above describe where it sits in the graph.
Author at: content/concepts/pyo3-ffi/card.ts