简介
RustPython/RustPython: A Python Interpreter written in Rust (github.com)
用户指南:https://rustpython.github.io/docs/
用 Rust 编写的 Python-3 (CPython >= 3.11.0) 解释器
作用
将python代码封装到rust编译器运行,提高运行速率(降低python编译器运行时内存线程的消耗过大)
安装
RustPython 需要 Rust 最新的稳定版本(例如 1 年 67 月 1 日的 7.2023.<>)。如果你不这样做 目前在您的系统上安装了 Rust,您可以按照 rustup.rs 中的说明进行操作。
要检查您当前运行的 Rust 版本,请使用 .如果您想更新,会将您的 Rust 安装更新到最新的稳定版本。rustc --version``rustup update stable
要在本地构建 RustPython,首先,克隆源代码:
git clone https://github.com/RustPython/RustPython
然后你可以切换到 RustPython 目录并运行演示(注意:是 需要防止 Windows 上的堆栈溢出):--release
$ cd RustPython
$ cargo run --release demo_closures.py
Hello, RustPython!
或者使用交互式外壳:
$ cargo run --release
Welcome to rustpython
>>>>> 2+2
4
注意:对于Windows用户,请在项目目录中将环境变量设置为路径。 (例如,当 RustPython 目录为 时,设置为RUSTPYTHONPATH``Lib``C:RustPython``RUSTPYTHONPATH``C:RustPythonLib
)
您还可以使用以下方法安装和运行 RustPython:
$ cargo install --git https://github.com/RustPython/RustPython
$ rustpython
Welcome to the magnificent Rust Python interpreter
>>>>>
(板条箱目前因外出而被从 crates.io 上拉出 日期,而不是在较新的 Rust 版本上构建;我们希望发布一个新的 版本即将推出™)rustpython-*
如果您想发出 https 请求,可以启用该功能,该功能 还允许您安装包管理器。请注意,在Windows上,您可以 需要安装 OpenSSL,或者您可以改为启用该功能, 它为您编译 OpenSSL,但需要 C 编译器、perl 和 .ssl``pip``ssl-vendor``make
一旦你安装了支持 SSL 的 rustpython,你可以通过以下方式安装 pip 运行:
cargo install --git https://github.com/RustPython/RustPython --features ssl
rustpython --install-pip
你也可以通过包管理器安装 RustPython。 这不受官方支持,可能已过时:conda
conda install rustpython -c conda-forge
rustpython
docker部署
wasm dockerfile
FROM rust:slim AS rust
WORKDIR /rustpython
USER root
ENV USER root
RUN apt-get update && apt-get install curl libssl-dev pkg-config -y &&
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
COPY . .
RUN cd wasm/lib/ && wasm-pack build --release
FROM node:alpine AS node
WORKDIR /rustpython-demo
COPY --from=rust /rustpython/wasm/lib/pkg rustpython_wasm
COPY wasm/demo .
RUN npm install && npm run dist -- --env.noWasmPack --env.rustpythonPkg=rustpython_wasm
FROM nginx:alpine
COPY --from=node /rustpython-demo/dist /usr/share/nginx/html
# Add the WASM mime type
RUN echo "types { application/wasm wasm; }" >>/etc/nginx/mime.types
rustpython dockerfile
FROM rust:latest as rust
WORKDIR /rustpython
COPY . .
RUN cargo build --release
FROM debian:stable-slim
COPY --from=rust /rustpython/target/release/rustpython /usr/bin
COPY --from=rust /rustpython/Lib /usr/lib/rustpython
ENV RUSTPYTHONPATH /usr/lib/rustpython
ENTRYPOINT [ "rustpython" ]
JIT(实时)编译器
RustPython 有一个非常实验性的 JIT 编译器,可以将 python 函数编译成原生代码。
build
默认情况下,JIT 编译器未启用,而是通过 cargo 功能启用。jit
cargo run --features jit
这需要安装 autoconf、automake、libtool 和 clang。
run
要编译函数,请调用它。__jit__()
def foo():
a = 5
return 10 + a
foo.__jit__() # this will compile foo to native code and subsequent calls will execute that native code
assert foo() == 15
在 Rust 中编写 Python 函数
在 RustPython 中,可以用 Rust 编写函数并将它们导入并从中调用它们 python。下面是一个示例:
fn rustmod_funct(
obj: PyObjectRef,
s: PyStringRef,
maybe_thing: OptionalArg<i32>,
vm: &VirtualMachine,
) -> PyResult<(String, Vec<u8>)> { ... }
参数
您可以使用实现 FromArgs
的任何类型作为函数的参数,其中包括 实现 TryFromObject
的类型。在我们的示例中,我们使用一个标准,a(实际上只是一个,并实现), 和 ,其中 OptionalArg
获取可选位置参数。在 此外,TryFromObject
是针对每种基元数字类型实现的,因此您可以将这些数字用作 参数也是。下面是实现 TryFromObject
的所有类型的列表。PyObjectRef``PyStringRef``PyRef<PyString>``PyRef``TryFromObject``OptionalArg<i32>
虚拟机参数
您可以选择在参数列表的末尾放置一个参数,以便 获取对 Python VM 的访问权限。如果你做的不仅仅是一个非常简单的功能,你可能会 想要这个,因为它是创建异常对象所必需的;但是,如果事实证明您没有使用它 在函数中,请始终记住您可以将其删除。vm: &VirtualMachine
返回类型
您可以将实现 IntoPyObject
的任何类型作为函数的返回类型,其中包括 许多简单的 Rust 类型,您希望将它们干净地映射到 Python,例如 -> , -> , 积分基元 -> , -> 浮点数。如果需要从函数返回错误,可以 将任何 IntoPyObject
类型作为 PyResult
中,并返回一个 .(您可以创建 使用虚拟机
上的方法之一的异常String``str``Vec<u8>``bytes``int``f32,f64``T``Err(exc)``new_*_error
)
编译到webassembly
RustPython/README.md at main · RustPython/RustPython · GitHub
测试案例
demo_closures.py
def foo(x):
def bar(z):
return z + x
return bar
f = foo(9)
g = foo(10)
print(f(2))
print(g(2))
原文始发于微信公众号(qingjiegong):rustpython:将python代码封装到rust编译器运行
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论