CoWasm-Python: Collaborative Python in WebAssembly for Servers and Browsers
URL: https://github.com/sagemathinc/cowasm/python/README.md
DEMOS:
https://cowasm.org - Python using SharedArrayBuffers
https://zython.org - Python using Service Workers
https://cowasm.sh - Dash Shell with Python, Sqlite, Lua, etc., using SharedArrayBuffers
Type this if you have nodejs at least version 16 installed:
What is this?
See https://github.com/sagemathinc/cowasm for more about the CoWasm project.
Try python-wasm
Try the python-wasm REPL under node.js (version at least 16):
Install python-wasm
Install python-wasm into your project, and try it via the library interface and the node.js terminal.
Then from the nodejs REPL:
There is also a Python REPL that is part of python-wasm:
You can also use python-wasm in your own web applications via webpack. In the browser, this transparently uses SharedArrayBuffers if available, and falls back to ServiceWorkers.
Build from Source
We support and regularly test building CoWasm-Python from source on the following platforms:
x86_64 macOS and aarch64 macOS (Apple Silicon M1)
x86_64 and aarch64 Linux
See the top level README.md file.
Benchmarks
There is a collection of cpu-intensive benchmarks in packages/bench/src, which you can run under various Python interpreters by running
Here are some grand total times that I recorded at some point in time in the past. Don't take them too seriously. That said, I chose the parameters of the benchmarks so a single benchmark doesn't unduly impact the results (e.g., it is easy to game any such benchmark by adjusting parameters).
Python | x86_64 Linux | MacOS M1 max | aarch64 Linux (docker on M1 max) |
---|---|---|---|
PyPy 3.9.x (Python reimplemented with a JIT) | 2997 ms | 2127 ms | 1514 ms (ver 3.6.9) |
pylang (Javascript Python -- see https://github.com/sagemathinc/pylang) | 6909 ms | 2876 ms | 4424 ms |
Native CPython 3.11 | 9284 ms | 4491 ms | 4607 ms |
WebAssembly CPython (python-wasm) | 23109 ms | 12171 ms | 12909 ms |
The quick summary is that in each case pypy is twice as fast as pylang (basically node.js), python-lang is twice as fast as cpython, and native cpython is about 2.5x as fast as python-wasm. However, when you study the individual benchmarks, there are some significant differences. E.g., in brython.py
there is a benchmark "create instance of simple class" and it typically takes 4x-5x longer in WebAssembly versus native CPython.
Contact
Email [email protected] or @wstein389 if find this interesting and want to help out.
CoWasm is an open source 3-clause BSD licensed project. It includes components and dependencies that may be licensed in other ways, but nothing is GPL licensed, except some code in the sagemath subdirectory (which nothing else depends on).