ALL INTELLIGENCE
Hacker News AIT2·Tools

Mizu Enables Zero-Copy Parallel R and Python Over Shared Memory

Original title: Mizu – zero-copy parallel R and Python over shared memory
62AI Score

Mizu builds lock-free channels and work-stealing task pools over POSIX shared memory and Win32 file mappings, cutting task round-trip latency between Python and R from roughly 100 microseconds to under 1 microsecond. Lists, dicts, NumPy arrays and Arrow tables can be shared as zero-copy views across the two languages, giving cross-language data science pipelines low-overhead, process-isolated parallelism.

Full Report

Contents6 sections

pymizu

<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/shikokuchuo/pymizu/main/docs/assets/logo-dark.svg"><img src="https://raw.githubusercontent.com/shikokuchuo/pymizu/main/docs/assets/logo.svg" align="right" width="240" alt="pymizu logo"/></picture>

Thread-speed async and parallel Python over shared memory.

A task round trip drops from ~100 µs over sockets to < 1 µs, so you can divide work as finely as you would with threads (see Benchmarks). Common data structures like lists, dicts, numpy arrays, and Arrow tables can be shared between Python and R as zero-copy views.

In the default CPython build, the GIL runs CPU-bound threads on one core at a time, so compute parallelism in Python usually means multiple processes. Processes also isolate failures: a worker that crashes does not take down the host with it.

Channels and work-stealing task pools run over POSIX shared memory (Linux, macOS) or Win32 file mappings (Windows): lock-free single-producer single-consumer rings with batched publication, spin-then-park waiting that stays out of the kernel on the hot path, and OS notification when a peer dies.

pymizu is built on libmizu, a C library for lock-free shared-memory IPC. mizu binds the same core for R, so Python and R can talk to each other.

  • Zero-copy numpy arrays and Arrow interchange (pyarrow, polars, pandas, duckdb)
  • Drop-in concurrent.futures.Executor — real Futures, wait, as_completed, asyncio.wrap_future
  • Event-driven asyncio — await tasks and maps, async for and recv_async() on channels — wake-ups as data arrives, no polling threads
  • Reproducible parallel random numbers (seed=) — identical results for any worker count or steal order
  • Nested parallel maps — a task can map over its own pool, deadlock-free

Lifecycle: experimental. The API is not yet stable and we may make changes in response to feedback.

Installation

Install from PyPI:

BASH
pip install pymizu

Or the development version from GitHub:

BASH
pip install git+https://github.com/shikokuchuo/pymizu

Requires Python 3.10 or later on a 64-bit platform. The extension compiles the vendored C core, so no system library is necessary.

Optional extras:

  • pymizu[numpy]: zero-copy array views and raw-tier array staging
  • pymizu[cloudpickle]: lambdas, closures, and local functions as pool tasks

To request an extra with the GitHub install, use pip install "pymizu[numpy] @ git+https://github.com/shikokuchuo/pymizu".

Quickstart

PYTHON
import pymizu

with pymizu.Pool.create(4) as pool:
    task = pool.submit(pow, 2, 16)
    print(task.collect(timeout=5))

Benchmarks

Communication overhead against the stdlib concurrent.futures pools (Apple M4 Pro, from benchmarks/mizu-stdlib-bench.py):

Against ProcessPoolExecutor (tasks run in separate processes, with pickled payloads):

BenchmarkpymizuProcessPoolExecutorSpeedup
Trivial task round trip0.7 µs94.2 µs135x
Pipelined throughput, 1 worker2,390,000 tasks/s18,500 tasks/s129x
Parallel map overhead, trivial function, 4 workers0.4 µs/elt*70.3 µs/elt176x
Parallel map of 2,000 ~5 µs tasks, 4 workers4.3 ms129 ms30x

Against ThreadPoolExecutor (tasks share one process, so the GIL caps CPU-bound work at a single core):

BenchmarkpymizuThreadPoolExecutorSpeedup
Trivial task round trip0.7 µs9.5 µs14x
Pipelined throughput, 1 worker2,390,000 tasks/s405,000 tasks/s5.9x
Parallel map overhead, trivial function, 4 workers0.4 µs/elt*2.6 µs/elt6.5x
Parallel map of 2,000 ~5 µs tasks, 4 workers4.3 ms50 ms12x

benchmarks/mizu-bench.py runs the pymizu rows standalone. See how pymizu compares with Ray, Dask, joblib, and modern CPython.

* elt = element; microseconds of wall time per map element.

Drop-in concurrent.futures

PoolExecutor adapts a pool to the stdlib concurrent.futures.Executor interface, so code written against ProcessPoolExecutor drops in. submit returns real Future objects, so wait / as_completed / asyncio.wrap_future all work unchanged:

PYTHON
with pymizu.PoolExecutor.create(4) as ex:
    futures = [ex.submit(pow, 2, i) for i in range(4)]
    print([f.result() for f in futures])

Parallel map

Pool.map(fn, x) maps fn over x on the pool and returns a Map handle immediately, just as Pool.submit() returns a task. collect() on the handle waits and returns the values in order. The function and data cross into shared memory once. Workers divide the elements among themselves, claiming ranges from a shared cursor.

A map also nests: the mapped function can map over its own pool with pymizu.current_pool(), deadlock-free.

PYTHON
with pymizu.Pool.create(4) as pool:
    m = pool.map(abs, range(-5, 5))
    print(m.collect())

R interop

Pool workers can be R processes that run the mizu package, the R binding of the same core. pymizu.r_launcher() spawns them and pymizu.call() specifies the task. Here, R's built-in mtcars is summarized in R and read back into polars:

PYTHON
import polars as pl
import pymizu

with pymizu.Pool.create(4, launcher=pymizu.r_launcher()) as pool:
    task = pool.submit(
        pymizu.call(source="aggregate(mpg ~ cyl, data = mtcars, FUN = mean)")
    )
    print(pl.DataFrame(task.collect()))
  • A numpy array arrives in R as a numeric vector, and a polars, pyarrow, or pandas frame as a data frame
  • A vector or data frame arrives back as a numpy array or a pymizu.Frame
  • Arrow data (pyarrow tables and record batch readers, or any __arrow_c_stream__ producer) crosses the same way

The R interop page has the full contract: the portable subset, pymizu.Frame, zero-copy frames, and the dtype matrix. The mizu README shows the reverse direction: an R host that drives Python workers with mizu_py_launcher().

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)