Skip to main content

Python extension to run WebAssembly binaries

Project description

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

A complete and mature WebAssembly runtime for Python based on Wasmer.

Features:

  • Easy to use: The wasmer API mimics the standard WebAssembly API,
  • Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
  • Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed,
  • Modular: wasmer can compile the WebAssembly modules with different engines or compiler.

Documentation: browse the detailed API documentation full of examples.

Examples as tutorials: browse the examples/ directory, it's the best place for a complete introduction!

Quick Introduction

The wasmer package brings the required API to execute WebAssembly modules. In a nutshell, wasmer compiles the WebAssembly module into compiled code, and then executes it. wasmer is designed to work in various environments and platforms: From nano single-board computers to large and powerful servers, including more exotic ones. To address those requirements, Wasmer provides 2 engines and 3 compilers.

Succinctly, an engine is responsible to drive the compilation and the execution of a WebAssembly module. By extension, a headless engine can only execute a WebAssembly module, i.e. a module that has previously been compiled, or compiled, serialized and deserialized. By default, the wasmer package comes with 2 headless engines:

  1. wasmer.engine.JIT, the compiled machine code lives in memory,
  2. wasmer.engine.Native, the compiled machine code lives in a shared object file (.so, .dylib, or .dll), and is natively executed.

Because wasmer does not embed compilers in its package, engines are headless, i.e. they can't compile WebAssembly module; they can only execute them. Compilers live in their own standalone packages. Let's briefly introduce them:

Compiler package Description PyPi
wasmer_compiler_singlepass Super fast compilation times, slower execution times. Not prone to JIT-bombs. Ideal for blockchains On PyPi Downloads
wasmer_compiler_cranelift Fast compilation times, fast execution times. Ideal for development On PyPi Downloads
wasmer_compiler_llvm Slow compilation times, very fast execution times (close to native, sometimes faster). Ideal for Production On PyPi Downloads

We generally recommend wasmer_compiler_cranelift for development purposes and wasmer_compiler_llvm in production.

Learn more by reading the documentation of the wasmer.engine submodule.

Install

To install the wasmer Python package, and let's say the wasmer_compiler_cranelift compiler, just run those commands in your shell:

$ pip install wasmer==1.0.0
$ pip install wasmer_compiler_cranelift==1.0.0

And you're ready to get fun!

Example

We highly recommend to read the examples/ directory, which contains a sequence of examples/tutorials. It's the best place to learn by reading examples.

But for the most eager of you, and we know you're numerous you mischievous, there is a quick toy program in examples/appendices/simple.rs, written in Rust:

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

After compilation to WebAssembly, the examples/appendices/simple.wasm binary file is generated. (Download it).

Then, we can excecute it in 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!

And then, finally, enjoy by running:

$ python examples/appendices/simple.py

Development

The Python extension is written in Rust, with pyo3 and maturin.

First, you need to install Rust and Python. We will not make you the affront to explain to you how to install Python (if you really need, check pyenv). For Rust though, we advise to use rustup, then:

$ rustup install stable

To set up your environment, you'll need just, and then, install the prelude of this project:

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

It will install pyo3 and maturin for Python and for Rust. It will also install virtualenv.

Then, simply run:

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

Supported platforms

We try to provide wheels for as many platforms and architectures as possible. For the moment, here are the supported platforms and architectures:

Platform Architecture Triple Packages
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

Notes:

  • 1 wasmer_compiler_singlepass does not support aarch64 for the moment
  • 2 wasmer_compiler_llvm is not packaging properly on Windows for the moment

Wheels are all built for the following Python versions:

  • Python 3.6,
  • Python 3.7,
  • Python 3.8.
  • Python 3.9.
Learn about the “fallback” py3-none-any wheel

py3-none-any.whl

A special wasmer-$(version)-py3-none-any wheel is built as a fallback. The wasmer libray will be installable, but it will raise an ImportError exception saying that “Wasmer is not available on this system”.

This wheel will be installed if none matches before (learn more by reading the PEP 425, Compatibility Tags for Built Distributions).

Testing

Build all the packages and run the tests:

$ just build-all
$ just test

What is WebAssembly?

Quoting the WebAssembly site:

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

About speed:

WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

About safety:

WebAssembly describes a memory-safe, sandboxed execution environment […].

License

The entire project is under the MIT License. Please read the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

wasmer_py310-1.0.0-cp310-none-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.10 Windows x86-64

wasmer_py310-1.0.0-cp310-cp310-manylinux_2_27_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64

wasmer_py310-1.0.0-cp310-cp310-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10 macOS 10.7+ x86-64

File details

Details for the file wasmer_py310-1.0.0-cp310-none-win_amd64.whl.

File metadata

  • Download URL: wasmer_py310-1.0.0-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.4

File hashes

Hashes for wasmer_py310-1.0.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 4b7fd682e924b84fd19f28277dbc9055fe7d2457afa750ab8ebc845824707ee9
MD5 8cb45c8a854f25805f046df0ecdfadba
BLAKE2b-256 37d06b18db6488c888e09595a30cc7d58572c1444aa111ac0b0a6041842744c0

See more details on using hashes here.

File details

Details for the file wasmer_py310-1.0.0-cp310-cp310-manylinux_2_27_x86_64.whl.

File metadata

  • Download URL: wasmer_py310-1.0.0-cp310-cp310-manylinux_2_27_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.10, manylinux: glibc 2.27+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.4

File hashes

Hashes for wasmer_py310-1.0.0-cp310-cp310-manylinux_2_27_x86_64.whl
Algorithm Hash digest
SHA256 0cda9a159431cb690792cf5ad4af50cbc8b470c0406aa115da913c6992cbef43
MD5 c66591ba1490ac77c260ae2369f3961d
BLAKE2b-256 c24e28ceb3ca29c2148a8d6924099026e4bccb3ee1fbb1b818f775e0096d7a4f

See more details on using hashes here.

File details

Details for the file wasmer_py310-1.0.0-cp310-cp310-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer_py310-1.0.0-cp310-cp310-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.10, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.4

File hashes

Hashes for wasmer_py310-1.0.0-cp310-cp310-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 fd29686a047207740a7a89c15d19c2608783f9c296d109111065675a5b8ef8d0
MD5 b02236e30fa320ef75305ee98c5e9364
BLAKE2b-256 772683341d12e91dd67f4f9f4fdc78f3f3369c2b5575b7fb0cbc1242909c5d96

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page