Skip to content

Latest commit

 

History

History
104 lines (75 loc) · 1.94 KB

File metadata and controls

104 lines (75 loc) · 1.94 KB

Zinc — Python Adapter

Zero-copy shared memory for Python via cffi. Includes optional NumPy integration for ML/data workflows.

Install

pip install zinc-shm

Requires libzinc_core.dylib / libzinc_core.so built and findable.

Building from source

cargo build --release --manifest-path core/Cargo.toml
pip install -e adapters/python

Usage

from zinc import SharedRegion

# Process A — create
r = SharedRegion.create("/my-data", 4096)
buf = r.as_buffer()
buf[0:8] = (42).to_bytes(8, 'little')
r.notify()

# Process B — open
r2 = SharedRegion.open("/my-data")
buf2 = r2.as_buffer()
value = int.from_bytes(buf2[0:8], 'little')
print(value)  # 42

NumPy integration

import numpy as np
from zinc import SharedRegion

r = SharedRegion.create("/tensor", 4096)
arr = r.as_numpy(dtype=np.float32)  # zero-copy!
arr[0] = 3.14159
arr[1] = 2.71828
r.notify()

API

SharedRegion.create(name, capacity)

Create a new shared region.

  • name: str — shm name
  • capacity: int — size in bytes (page-aligned)

SharedRegion.open(name)

Open an existing shared region.

region.as_buffer() → memoryview

Zero-copy memoryview of the shared region.

region.as_numpy(dtype=np.float32) → numpy.ndarray

Zero-copy NumPy array view.

region.notify()

Signal all waiters.

region.wait(timeout_ms=1000) → bool

Block until notified.

region.close()

Release the handle. Use as context manager with with:

with SharedRegion.create("/data", 4096) as r:
    arr = r.as_numpy(dtype=np.float64)
    arr[0] = 1.0

Testing

pytest adapters/python/tests/ -v

Publish

cd adapters/python
pip install build twine
python -m build
twine upload dist/*

Platform support

OS Status
Linux
macOS

Windows is not supported. Zinc requires POSIX shm_open + mmap.