TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Calling Rust from Python using PyO3

144 pointsby loigeover 3 years ago

14 comments

rdedevover 3 years ago
Check out apache arrow in rust for transferring large amounts data between rust and python. With it you can get zero copy transfers between the languages. Afaik there is some performance penalty involved when converting python types to native rust types.<p>It used to be hard to do such transfers between rust and python using arrow but from v3 or v4, arrow implementation in rust started supporting the c data interface. Creating the array in rust and sending it to python might involve leaking a boxed object though<p>Check this out if you want to see how <a href="https:&#x2F;&#x2F;github.com&#x2F;jhoekx&#x2F;python-rust-arrow-interop-example&#x2F;blob&#x2F;master&#x2F;src&#x2F;lib.rs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jhoekx&#x2F;python-rust-arrow-interop-example&#x2F;...</a>
评论 #29369537 未加载
oconnor663over 3 years ago
<a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;blake3" rel="nofollow">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;blake3</a> has been based on PyO3 for about a year and a half now, and it&#x27;s been smooth sailing. One of the reasons we reach for compiled code from Python is to get multithreading working for CPU-bound tasks, and the combination of PyO3 plus Rayon on the Rust side is especially nice.
Grollicusover 3 years ago
I recently wanted to use some rust crates from Python and decided to give PyO3 a go and I must say it works really well.<p>Took about one evening to plug it all together and get it running. The guide at pyo3.rs was really helpful and maturin to build binary packages just works. You pass in python types and they arrive as rust-native types. Makes writing code feel very native.<p>The result: <a href="https:&#x2F;&#x2F;github.com&#x2F;Grollicus&#x2F;pyttfwrap" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Grollicus&#x2F;pyttfwrap</a> takes a string and splits it as it would wrap when rendered with a given TTF font. Most of the code is about keeping a reference to the loaded font as that&#x27;s an expensive operation.<p>Was some great fun and I&#x27;m sure I&#x27;ll use it some more.
wheelerof4teover 3 years ago
This is smart thinking. Python is the world&#x27;s best glue language and Rust is on track to become the world&#x27;s best compiled, safe language.<p>It makes sense to combine the two and replace C in this.
评论 #29373847 未加载
评论 #29369076 未加载
gravypodover 3 years ago
I hope someone creates a rule set for Bazel to automate this stuff. This seems like a really nice way to start rewriting performance critical code in a safe language!
评论 #29369769 未加载
评论 #29369048 未加载
vlmutoloover 3 years ago
We used PyO3 at $work to expose a Rust implementation of a compute-intensive algorithm to an existing Python codebase.<p>The teammate who did it had been using Rust only for a couple months and none of us had ever used PyO3. He got it done in just a couple days. I consider that an endorsement of the API they&#x27;ve built.<p>It&#x27;s heavily macro-based, which does cause some confusion. But if you spend some time with their examples, finding the fast path isn&#x27;t too tricky.
评论 #29373292 未加载
crm416over 3 years ago
We&#x27;ve been using PyO3 and Maturin at Spring for a while now, and happily. The smooth Python interop means we can call out to Rust without much pain for performance-critical codepaths. But the other side-effect is that we can use Rust across the org more broadly, even when Python interop isn&#x27;t a consideration -- e.g., for isolated services, or applications that need to compile to Windows, or whatever else -- since we&#x27;re building up the cultural knowledge and shared libraries to do so.
stabblesover 3 years ago
PyO3 is a clever name
评论 #29368988 未加载
dunefoxover 3 years ago
For Julia there&#x27;s pycall: <a href="https:&#x2F;&#x2F;github.com&#x2F;JuliaPy&#x2F;PyCall.jl" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JuliaPy&#x2F;PyCall.jl</a> I believe there&#x27;s one for the opposite direction as well.
staticassertionover 3 years ago
I&#x27;m curious about any rough edges.<p>1. What happens if the Rust code is misbehaving, panicking, etc?<p>2. What&#x27;s build support like for this?<p>I&#x27;d love to gut what little Python code I have left and use Rust.
评论 #29373717 未加载
s-zengover 3 years ago
<a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;dhall&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;dhall&#x2F;</a> uses PyO3 to reuse Rust&#x27;s implementation to allow Python to load and dump Dhall configs, since no good and performant native python implementations exist. It&#x27;s been quite pleasant for this purpose.
m3047over 3 years ago
This is actually a deserializer which I wrote. I would have done it in C but I stumbled on PyO3 and gave it a go: <a href="https:&#x2F;&#x2F;github.com&#x2F;m3047&#x2F;pyo3-deserializer" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;m3047&#x2F;pyo3-deserializer</a>
JPKabover 3 years ago
I&#x27;ve been waiting a while for a good article on integrating rust into python for a while. I was inspired by the amazing performance of the orjson python lib.
marvinvzover 3 years ago
I&#x27;ve been playing around with Rust, PyO3 and Blender the last few days and it has been really nice to use.