Contribute Media
A thank you to everyone who makes this possible: Read More

Let’s embrace WebAssembly!

Description

WebAssembly (WASM) is an open, low level binary format designed to be compact and run at native speed, while being memory-safe. WASM is primarily intended to run code in browsers, but its by no means limited to this. This makes it an interesting intermediate language (IR); code that compiles to WASM will (in the future) run basically anywhere. In short: WASM is coming and its great!

Unsurprisingly, WASM is being embraced by many communities, such as C++, Rust, Lua, and .NET. Sadly, there does not seem to be a lot of enthusiasm from the Python community yet…

Perhaps this is because Python is interpreted and can therefore not (easily) use WASM as a compilation target. It should be possible to compile a Python interpreter (like CPython or Pypy) to WASM and thereby run Python code in a browser. But the result would be pretty heavy-weight, so it would arguably not be a very practical.

Within the PPCI project (a pure Python compiler infrastructure) tooling has been developed to load, inspect, compile and even run WASM modules. This allows combining WASM and Python in new ways. In this talk we discuss two approaches by which the Python community might embrace WASM.

Firstly, various projects already exist that compile Python functions to other languages (e.g. Numba, PScript), and we have type annotations. If Python functions would be compiled to WASM, the resulting code would run in any WASM runtime (e.g. the browser). This will be demonstrated with an example, for which the resulting code can be run either in a browser or inside Python itself. In either case, it’s pretty darn fast.

Secondly, rather than using WASM, it might be interesting for Python to function as a platform to run and bind WASM modules. Two of Python’s greater strengths are its rich ecosystem and its ability to glue things together. Let’s build on that! Imagine creating an application that consists of multiple WASM modules, perhaps compiled from different languages, and binding these together into a single Python app.

This will be demonstrated with a game, which is written in Rust, compiled to WASM, and running inside a Python process. The WASM module consumes a drawing API, which is in this case provided by Python, and Python feeds the WASM module with user input.

I hope that this talk inspires other Pythonistas to think about the advantages that WASM can bring to our ecosystem, and also about the role that Python can play in the growing WASM ecosystem.

Details

Improve this page