← Back to Tech Radar
Hacker News tech

Libraries Run Rust Inside Python (With PyO3)

Trending on Hacker News: Libraries Run Rust Inside Python (With PyO3) (49 points / 29 comments, via belderbos.dev)

In one line

Build a Rust JSON parser into a module Python can import with PyO3, then see what the return trip costs when Rust data becomes Python dicts and lists.

Opening excerpt

Every time you validate data with Pydantic v2, the data-validation library most Python apps reach for, a Rust extension does the work. Its core, pydantic-core, is built with PyO3, the same toolchain we’ll use here.

This post builds that same kind of bridge, small enough to read in one sitting: a JSON parser written in Rust, exposed to Python, so you can import it like any other package. The last step, turning the Rust result into Python objects, is the one to understand before you port anything: for a parser like this, it can cost more than the parsing itself.

#[pyfunction] and #[pymodule] are the two Rust macros that do the wiring. A Rust attribute macro is close to a Python decorator: it rewrites the function it sits on, here adding the glue that lets Python call it and handles the type conversions and reference counting at the boundary.

Maturin then compiles the crate to a shared library (.so, .dylib, .dll) and drops it into your virtual environment, so import just works.

(Excerpted from the original; full article via the source link below.)

This story hit the Hacker News front page today (49 points / 29 comments, via belderbos.dev). Our Tech Radar aggregates daily signals on AI engineering, backend architecture and DevOps — browse the related services and further reading below, or get in touch with our team.

Source: Hacker News

Related Services

Related Reading