Skip to content

Python & WASM

Why ? (Some use cases)

  • Template engine that works both on server and browser
  • Apps that work “on the edge” (e.g. Cloudflare -> need to better understand the technology involved)

  • Discussion w/ Paul Everitt:

(From 2019):

Russell’s talk has a very good analogy about “winged keel” in yacht racing, which he applies to WASM. On the other hand, it isn’t clear how it will manifest. Lukazs makes a strong case that we shouldn’t do the Python runtime in the browser, but should translate to as native WASM as we can on the server in a build step.
Namely, we’ll never get 60 fps in the browser if there’s a Python runtime and compiler there, even in WASM.
Lukazs mentioned Cython but was more interested in mypyc as part of a toolchain that generated WASM. Any thoughts?

(Dec. 2021):

OTOH, I really would like to leave the door open to generate client-side template logic that can work in either server-side Python or client-side JS/WASM. Jinja has a language which isn’t Python. It’s an AST of expressions.
I think it’s too early to tell if wasmer for Python will be viable to live with. But it’s fun to imagine a WASM-based engine that worked in Python and the browser, with templating and the things that go with it — state model, injection, etc.

Issues / limitations

  • Need for a runtime. Hopefully small (but probably won’t be). How can that be competitive with a JS program that can tap freely into the JS runtime ?

Examples / inspiration


Potential leads

Compiler technologies

Next steps

  • Take Tinypy/tpythonpp/Airtight + some WASM backend (PPCI or Pybynaryen)

And see where it leads.

Dev log

2021/10/01: playing with PPCI again

New repo with some experiments:

2020/12/21: Simple WASM experiment

A tiny subset of Python can be compiled to WASM using PPCI:

  • Install PPCI
  • Write a Python module ( with only one function, no annotation, only simple arithmetics
  • Use ppci.lang.python.python_to_wasm
  • Run it wih wasmer run playground/mandelbrot.wasm -i mandelbrot

=> Result: quite fast (300ms vs. 180ms for the C version).

- Investigate other Py->WASM compilers (are there any ?)
- Benchmark other WASM runtimes

See also


Back to top