Rust 和 Python 如何混合使用?

看到一个 Python 的高性能 json 序列化库 —— orjson

orjson is a fast, correct JSON library for Python. It benchmarks as the fastest Python library for JSON and is more correct than the standard json library or other third-party libraries. It serializes dataclass, datetime, numpy, and UUID instances natively.

但是发现了这个库用了 rustpython 混合编程!!!

图片.png

Python 如何调用 Rust 代码?

  • 单独起一个 rust 进程然后 IPC 通讯?
  • Web Socket 通讯?
  • jit
阅读 4.4k
2 个回答

我知道了,原理就是

  • rust -> c API
  • python <- c API

有一个专门的库:pyo3

Rust bindings for Python, including tools for creating native Python extension modules. Running and interacting with Python code from a Rust binary is also supported.

图片.png

它哪里调用rust代码了?只是使用了rust代码编写了底层的类库而已。rust编译是静态编译的,直接构建出平台相关的二进制包而已。

Numpy的搞法也是底层的类库直接使用C编写,构建出平台相关的二进制包,这个类库只不过用rust编写了类库而已

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题