Wasmer's py2wasm Compiles Python To WebAssembly
Written by Nikos Vaggalis   
Thursday, 16 May 2024

py2wasm is a compiler that turns your Python code into WebAssembly, "running it at 3x faster speeds".

wasmerlogo

Let's make something clear first. The official announcement from Wasmer states that by turning you program to Webassembly with py2wasm, it runs at "3x faster speeds". What it important to understand is this is in comparison to CPython WASM (CPython interpreter inside of WebAssembly), not native CPython. As such, it is an improvement over the current state of running Python programs in WebAssembly.

With that out of the way, lets get into the details.

Py2wasm is actually a fork of the Nuitka compiler which compiles Python into C, but adapted to emitting Webassembly instead. The fork, however, exists initially as Wasmer envisions py2wasm as a thin layer over Nuitka. To prove that, they have went to the extend to open a PR on Nuitka's Githubrepo.

pywasm

Using Nuitka seemed like the easiest option to speed up to Python in WebAssembly contexts, mainly because most of the hard work was already done to transpile Python Code into the underlying CPython interpreter calls, so with just a few tweaks the developers could get it working to compile to WebAssembly. Other options Wasmer examined before they decided to use  Nuitka, were

  • Compile a Python subset to Wasm, using CPython, RPython and Codon.
  • Running a JIT in WebAssembly with PyPy.
  • Static Analysis to analyze/autodetect the typings of programs ahead of time, so that the code could be transpiled into more performant specializations. Candidates were mypy, mypy-c and Nuitka.

And the winner was Nuitka because Nuitka works, as already mentioned, by transpiling the Python calls that the program makes into C, using the inner CPython API calls. With a few tweaks it has been made to emit WebAssembly executables. This means that you can run Python programs not just inside the browser but also in targets where WebAssembly WASI is supported.

As for WASI, it is the WebAssembly System Interface, a modular system interface for WebAssembly (Wasm) that enables developers to run Wasm programs on any platform, including the browser, the cloud, and the edge. WASI is designed to be secure, fast, and portable, allowing developers to write their programs once and run them anywhere. In Wasmer's case the use case is moving their Python Django backend from Google Cloud into Wasmer Edge.

Here is how you can use p2wasm :

$ pip install py2wasm
$ py2wasm myprogram. py -o myprogram. wasm
$ wasmer run myprogram. wasm

but after you have installed the Wamer runtime, which you can do on Linux as:

curl https://get.wasmer. io -sSfL | sh

and on Windows with Powershell as:

https://win.wasmer. io -useb | iex

Another prerequisite is getting hold of a C compiler. While that is standard on Linux, on Windows Nuitka works with MinGW-w64, MSVC (Visual Studio), and Clang (via Visual Studio).

One caveat is that py2wasm needs to run in a Python 3.11 environment and does not (yet) support Python 3.12.

With that said, Wasmer are working to make py2wasm available as a Wasmer package in order to make it even easier to use. 

wasmericon

More Information

Announcing py2wasm: A Python to Wasm compiler 

Related Articles

The WinterJS Javascript Runtime Is Asking For Your Attention 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Ilya Sutskever Raise $1 Billion To Research Safe AI
11/09/2024

Safe Superintelligence, the company co-founded by Ilya Sutskever after he resigned from Open AI, has raised $1billion in cash funding, despite the fact that it intends not to release any products [ ... ]



Watch The Oregon Programming Language Summer School Lectures
20/09/2024

Each year, the Oregon Programming Language Summer School covers topics in the foundations of programming languages. All the recordings of the lectures that took place during this year's event are now  [ ... ]


More News

kotlin book

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 16 May 2024 )