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

References

Potential leads

Compiler technologies

Next steps

  • Take Tinypy/tpythonpp/Airtight + some WASM backend (PPCI or Pybynaryen)
    • github.com/philhassey/tinypy
    • gitlab.com/hartsantler/tpythonpp
    • github.com/alehander92/Airtight

And see where it leads.

Dev log

2021/10/01: playing with PPCI again

New repo with some experiments: https://github.com/abilian/ppci-sandbox

2020/12/21: Simple WASM experiment

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

  • Install PPCI https://pypi.org/project/ppci/
  • Write a Python module (mandelbrot.py) 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).

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

See also

Notes

Back to top