跳转到主要内容

运行WebAssembly二进制的Python扩展

项目描述

Wasmer logo Wasmer Python PyPI version Wasmer Python Documentation Wasmer PyPI downloads Wasmer Slack Channel

基于 Wasmer 的完整且成熟的 WebAssembly 运行时,适用于 Python。

特性

  • 易于使用wasmer API 仿照标准的 WebAssembly API
  • 快速wasmer 尽可能快地执行 WebAssembly 模块,接近 原生速度
  • 安全:所有对 WebAssembly 的调用都将快速执行,更重要的是,它们是完全安全和沙箱化的
  • 模块化wasmer 可以使用不同的引擎或编译器编译 WebAssembly 模块

文档:浏览详细的 API 文档 https://wasmerio.github.io/wasmer-python/api/wasmer/wasmer.html,其中包含大量示例

示例 作为教程:浏览 examples/ 目录,这是完整介绍的绝佳地方!

快速介绍

wasmer 包提供了执行 WebAssembly 模块所需的 API。简而言之,wasmer 将 WebAssembly 模块编译成编译后的代码,然后执行它。 wasmer 设计用于在各种环境和平台上工作:从小型单板计算机到大型强大的服务器,包括更多异类平台。为了满足这些要求,Wasmer 提供了 2 个引擎和 3 个编译器。

简而言之,引擎 负责驱动 WebAssembly 模块的 编译执行。通过扩展,无头 引擎只能执行 WebAssembly 模块,即已经编译或编译、序列化和反序列化的模块。默认情况下,wasmer 包包含 2 个无头引擎

  1. wasmer.engine.JIT,编译后的机器代码驻留在内存中
  2. wasmer.engine.Native,编译后的机器代码驻留在共享对象文件(.so.dylib.dll)中,并原生执行

由于 wasmer 在其包中不嵌入编译器,因此引擎是无头的,即它们不能编译 WebAssembly 模块;它们只能执行它们。编译器存在于它们自己的独立包中。让我们简要介绍它们

编译器包 描述 PyPi
wasmer_compiler_singlepass 编译时间超级快,执行时间较慢。不易受到 JIT-bombs 影响。非常适合区块链 On PyPi Downloads
wasmer_compiler_cranelift 编译时间和执行时间都很快。非常适合开发 On PyPi Downloads
wasmer_compiler_llvm 编译时间慢,执行时间非常快(接近原生,有时更快)。非常适合生产 On PyPi Downloads

我们通常推荐使用 wasmer_compiler_cranelift 进行开发,并在生产中使用 wasmer_compiler_llvm

通过阅读 wasmer.engine 子模块的文档 了解更多。

安装

要安装 wasmer Python 包,以及 wasmer_compiler_cranelift 编译器,只需在您的 shell 中运行以下命令

$ pip install wasmer==1.1.0
$ pip install wasmer_compiler_cranelift==1.1.0

然后您就可以开始享受乐趣了!

示例

我们强烈建议您阅读 examples/ 目录,其中包含一系列示例/教程。这是通过阅读示例学习最佳的地方!

但对于你们中最渴望的,我们知道你们为数不少,顽皮的你们,在 examples/appendices/simple.rs 中有一个快速的玩具程序,用 Rust 编写。

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

编译成 WebAssembly 后,生成了 examples/appendices/simple.wasm 二进制文件。(下载它)。

然后,我们可以在 Python 中执行它。

from wasmer import engine, Store, Module, Instance
from wasmer_compiler_cranelift import Compiler

# Let's define the store, that holds the engine, that holds the compiler.
store = Store(engine.JIT(Compiler))

# Let's compile the module to be able to execute it!
module = Module(store, open('simple.wasm', 'rb').read())

# Now the module is compiled, we can instantiate it.
instance = Instance(module)

# Call the exported `sum` function.
result = instance.exports.sum(5, 37)

print(result) # 42!

最后,运行并享受。

$ python examples/appendices/simple.py

开发

Python 扩展是用 Rust 编写的,使用了 pyo3maturin

首先,您需要安装 Rust 和 Python。我们不会冒犯您,向您解释如何安装 Python(如果您真的需要,请查看 pyenv)。对于 Rust,我们建议使用 rustup,然后

$ rustup install stable

为了设置您的环境,您需要 just,然后安装此项目的先导程序。

$ cargo install just
$ just --list # to learn about all the available recipes
$ just prelude

这将安装 Python 和 Rust 的 pyo3maturin。它还将安装 virtualenv

然后,只需运行

$ source .env/bin/activate
$ just build api
$ just build compiler-cranelift
$ python examples/appendices/simple.py

支持的平台

我们试图为尽可能多的平台和架构提供轮子。目前,以下是支持的平台和架构:

平台 架构 三重
Linux amd64 x86_64-unknown-linux-gnu wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm
aarch64 aarch64-unknown-linux-gnu wasmer
wasmer_compiler_singlepass 1
wasmer_compiler_cranelift
wasmer_compiler_llvm
Darwin amd64 x86_64-apple-darwin wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm
Windows amd64 x86_64-pc-windows-msvc wasmer
wasmer_compiler_singlepass
wasmer_compiler_cranelift
wasmer_compiler_llvm 2

说明

  • 1 wasmer_compiler_singlepass 目前不支持 aarch64
  • 2 wasmer_compiler_llvm 目前在 Windows 上打包不正确

轮子是为以下 Python 版本构建的:

  • Python 3.7
  • Python 3.8
  • Python 3.9
  • Python 3.10
了解“回退” py3-none-any 轮子

py3-none-any.whl

专门构建了一个 wasmer-$(version)-py3-none-any 轮子作为回退。该 wasmer 库将可安装,但会引发一个表示“该系统上不可用”的 ImportError 异常。

如果之前没有匹配项,则将安装此轮子(通过阅读 PEP 425,构建分发的兼容性标签 了解更多)。

测试

构建所有包并运行测试

$ just build-all
$ just test

什么是 WebAssembly?

引用 WebAssembly 网站

WebAssembly(简称 Wasm)是一种基于堆栈的虚拟机的二进制指令格式。Wasm 被设计为一种可移植的目标,用于编译高级语言如 C/C++/Rust,使得部署在客户端和服务器应用程序的 Web 上成为可能。

关于速度

WebAssembly 通过利用广泛平台上可用的 常见硬件功能 来实现以原生速度执行。

关于安全性

WebAssembly 描述了一个内存安全、沙盒化的 执行环境 [...]

许可证

整个项目都在 MIT 许可证下。请阅读 LICENSE 文件

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅生成分发存档的教程

构建分发

wasmer-1.1.0-py3-none-any.whl (1.6 kB 查看哈希值)

上传时间: Python 3

wasmer-1.1.0-cp310-none-win_amd64.whl (1.4 MB 查看哈希值)

上传时间: CPython 3.10 Windows x86-64

wasmer-1.1.0-cp310-cp310-manylinux_2_24_x86_64.whl (1.6 MB 查看哈希值)

上传时间: CPython 3.10 manylinux: glibc 2.24+ x86-64

wasmer-1.1.0-cp310-cp310-macosx_10_7_x86_64.whl (1.5 MB 查看哈希值)

上传时间: CPython 3.10 macOS 10.7+ x86-64

wasmer-1.1.0-cp39-none-win_amd64.whl (1.4 MB 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

wasmer-1.1.0-cp39-cp39-manylinux_2_24_x86_64.whl (1.6 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.24+ x86-64

wasmer-1.1.0-cp39-cp39-macosx_11_0_arm64.whl (1.3 MB 查看哈希值)

上传时间: CPython 3.9 macOS 11.0+ ARM64

wasmer-1.1.0-cp39-cp39-macosx_10_7_x86_64.whl (1.5 MB 查看哈希值)

上传时间: CPython 3.9 macOS 10.7+ x86-64

wasmer-1.1.0-cp38-none-win_amd64.whl (1.4 MB 查看哈希)

上传时间 CPython 3.8 Windows x86-64

wasmer-1.1.0-cp38-cp38-manylinux_2_24_x86_64.whl (1.6 MB 查看哈希)

上传时间 CPython 3.8 manylinux: glibc 2.24+ x86-64

wasmer-1.1.0-cp38-cp38-macosx_10_7_x86_64.whl (1.5 MB 查看哈希)

上传时间 CPython 3.8 macOS 10.7+ x86-64

wasmer-1.1.0-cp37-none-win_amd64.whl (1.4 MB 查看哈希)

上传时间 CPython 3.7 Windows x86-64

wasmer-1.1.0-cp37-cp37m-manylinux_2_24_x86_64.whl (1.6 MB 查看哈希)

上传时间 CPython 3.7m manylinux: glibc 2.24+ x86-64

wasmer-1.1.0-cp37-cp37m-macosx_10_7_x86_64.whl (1.5 MB 查看哈希)

上传时间 CPython 3.7m macOS 10.7+ x86-64

由以下机构支持