Compiling Python code in WebAssembly: how to do it and what is the use

Compiling Python code in WebAssembly: how to do it and what is the use

WebAssembly (Wasm) is designed to be a portable and platform-independent bytecode format. This means that code compiled in Wasm can run on a variety of environments, including web browsers, servers, IoT devices, and more. Compiling languages come Python in Wasm allows you to easily run your code on a wide range of devices and environments, without having to adapt it for each platform.

Wasm is revolutionizing the Web because, for example, it offers performance vastly superior to interpreted languages ​​like JavaScript. Wasm also allows the use of a wide range of programming languages, including C, C++, Rust and now Python, for develop web applications. This allows developers to use the tools and languages ​​best suited to their needs, without having to compromise performance and functionality.

How to compile Python code in WASM

One of the most popular languages ​​out there is Python: While it is certainly possible to run Python programs in WebAssembly, overall performance is pretty poor.

Thus, the authors of the project Wasmer, started five years ago with the specific objective of transforming code created with any programming language into WebAssembly, today announce an important innovation. The new py2wasm compiler “translates” Python programs into Wasm (thanks to night) allowing therunning three times faster compared to the base interpreter.

How to use py2wasm

To use the compiler py2wasm from Python to Wasm, Python 3.11 must already be present on the system in use. To easily set up Python, you can resort to pyenv:

pyenv install 3.11 && pyenv global 3.11

The following commands allow you to instead install the compilerrequest a Python script to be compiled into Wasm bytecode and then run with Wasmer:

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

How to speed up Python code execution with Wasm

Several strategies can be used to optimize Python workloads in WebAssembly. The various applicable schemes are essentially three.

You can limit yourself to using a subset of Python, more easily translated into high-performance Wasm code; you can activate the JIT compiler (Just-in-Time) within Python in order to optimize and compile the code during execution; finally you can use thestatic analysis to optimize the generated code.

The authors of Wasmer and the py2wasm compiler explain that the last way gives excellent results. Static analysis, in fact, allows you to automatically analyze and detect the typings of the program in advance, so the code can be rendered with a more performing approach (usually through Python transposition to C).

The typifications refer to the practice of assigning a specific data type to a variable, function, or value. This data type defines the type of value that the variable can hold or return and specifies how that value can be manipulated.

Static analysis, the process by which the source code is examined for errors, inefficiencies or areas for improvement without actually executing the program, is managed by night (mentioned previously). As mentioned, Nuitka works by transposing the Python calls that the program makes to C, using internal API calls.

It can even work as code obfuscatorto make it impossible to decompile the Wasm program.

There is still work to do

The initiators of the Wasmer project admit that in the case of Python there is still work to be done. nightfor example, is still not compatible with Python 3.12 and several low-level changes were necessary to manage, for example, cached values ​​and speed up retrieval, especially on 64-bit platforms.

Once all remaining problems have been fixed, the py2wasm compiler will be added to the list of available Wasmer packages: in this way, a single simple command will be enough to make it work.

In any case, an important milestone has already been laid and the possibility of run Python code from browser From today the Web opens up practically boundless horizons.

The opening image is created by Wasmer.

Leave a Reply

Your email address will not be published. Required fields are marked *