|
| 1 | +# 🐍⚡ pyodide-pool |
| 2 | + |
| 3 | +Run Python callables and dask graphs in parallel on a pool of |
| 4 | +[Pyodide](https://pyodide.org) Web Workers. |
| 5 | + |
| 6 | +🔗 [Live Demo](https://fideus-labs.github.io/python-wasm-parallelism/) · |
| 7 | +[Source](https://github.com/fideus-labs/python-wasm-parallelism) |
| 8 | + |
| 9 | +A single Pyodide interpreter is strictly single-threaded (no `-pthread`, no |
| 10 | +`fork`), so parallelism means *many interpreters*. `pyodide-pool` is the |
| 11 | +driver-side Python API for a pool of Web Workers (browser) or |
| 12 | +`worker_threads` (Node), each hosting its own Pyodide interpreter: |
| 13 | + |
| 14 | +- **`submit`** — run one cloudpickled call on a worker and await its result. |
| 15 | +- **`compute` / `get`** — an async dask scheduler that executes |
| 16 | + `dask.delayed` / `dask.bag` / dask-array graphs in parallel on the pool. |
| 17 | +- **Package mirroring** — packages loaded in the driver (e.g. numpy) are |
| 18 | + installed on each worker on first use. |
| 19 | +- **Remote errors** — the original Python exception is re-raised on the |
| 20 | + driver with the worker traceback chained. |
| 21 | + |
| 22 | +The package runs only in the driver Pyodide instance; workers never install |
| 23 | +it. It is async-first: blocking the driver would starve the event loop the |
| 24 | +pool lives on. |
| 25 | + |
| 26 | +## Usage |
| 27 | + |
| 28 | +```python |
| 29 | +import dask |
| 30 | +import pyodide_pool |
| 31 | + |
| 32 | +# In JupyterLite: import the JS bundle and start 4 nested workers |
| 33 | +pool = await pyodide_pool.create_pool(pool_size=4) |
| 34 | + |
| 35 | +value = await pyodide_pool.submit(fn, *args, **kwargs) # one remote call |
| 36 | +total = await pyodide_pool.compute(dask.delayed(sum)([...])) # dask collections |
| 37 | +``` |
| 38 | + |
| 39 | +`create_pool` loads the self-contained JS bundle from the JupyterLite site by |
| 40 | +default; pass `js_url=` to load it from elsewhere. A web app that embeds |
| 41 | +Pyodide can instead create the JS `PyodidePool` itself and register it with |
| 42 | +`pyodide.registerJsModule('js_pyodide_pool', { pool })`. |
| 43 | + |
| 44 | +Install `pyodide-pool[dask]` for `compute` / `get`. |
| 45 | + |
| 46 | +For the multiprocessing-style API, see |
| 47 | +[`wasm-multiprocessing`](https://github.com/fideus-labs/python-wasm-parallelism/tree/main/python/wasm_multiprocessing). |
| 48 | + |
| 49 | +## Documentation |
| 50 | + |
| 51 | +- [Project README](https://github.com/fideus-labs/python-wasm-parallelism#readme) |
| 52 | +- [dask scheduler design](https://github.com/fideus-labs/python-wasm-parallelism/blob/main/docs/architecture/dask-scheduler-design.md) |
| 53 | +- [Benchmarks](https://github.com/fideus-labs/python-wasm-parallelism/tree/main/docs/benchmarks) |
| 54 | + |
| 55 | +## License |
| 56 | + |
| 57 | +MIT |
0 commit comments