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:
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?
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.
Discussion with Robert Smallshire regarding this: https://twitter.com/robsmallshire/status/1477004769298456580
Full-stack web app, à la: https://www.sheshbabu.com/posts/rust-wasm-yew-single-page-application/
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¶
- List of languages that run on WASM (not all compiled): https://github.com/appcypher/awesome-wasm-langs
- AssemblyScript = variant of Typescript with Binaryen as the backend. Large project.
Recent (2022/04) links¶
- https://speakerdeck.com/tiran/language-summit-2022-webassembly-python-in-the-browser-and-beyond “Language Summit 2022: WebAssembly: Python in the browser and beyond”
- https://github.com/pyscript/pyscript “PyScript is a Pythonic alternative to Scratch, JSFiddle or other “easy to use” programming frameworks, making the web a friendly, hackable, place where anyone can author interesting and interactive applications.”
- (Books about WebAssembly)
- https://github.com/WebAssembly/binaryen (Compiler infrastructure and toolchain library for WebAssembly - in C++)
- There is a Python binding: https://pypi.org/project/pybinaryen/
- PPCI - has a WASM backend. Not active currently.
- MIR (NB: no WASM support currently)
- QBE Intermediate Language (No WASM backend).
- Take Tinypy/tpythonpp/Airtight + some WASM backend (PPCI or Pybynaryen)
And see where it leads.
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
- 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