Skip to content

Latest commit

 

History

History
59 lines (43 loc) · 1.84 KB

File metadata and controls

59 lines (43 loc) · 1.84 KB

Python bindings for shr

A pure-stdlib ctypes wrapper over libshr. Nothing to install: if libshr.so is on the system (via cmake --install), import shr just works. To run against a build tree before installing, point SHR_LIB at the built library:

SHR_LIB=../../../build/src/libshr.so python3 -c "import shr; print(shr.__doc__)"

Quick start

import shr

# create a 1 MiB ring
shr.create("/dev/shm/demo", 1 << 20)

# writer
with shr.Shr.open("/dev/shm/demo", shr.SHR_WRONLY) as w:
    w.write(b"hello")

# reader
with shr.Shr.open("/dev/shm/demo", shr.SHR_RDONLY) as r:
    msg = r.read()          # -> b"hello"

Each write sends one message; each read returns exactly one message (boundaries preserved). Open non-blocking with SHR_RDONLY | SHR_NONBLOCK and use Shr.fileno() with select/selectors for readiness notification.

Tests

Stdlib unittest, no extra packages. From this directory:

python3 -m unittest          # run all tests
python3 -m unittest -v       # one line per test

If libshr.so isn't installed, the tests fall back to ../../../build/src/libshr.so automatically; point SHR_LIB at it if your build lives elsewhere:

SHR_LIB=/path/to/libshr.so python3 -m unittest

The tests exercise the binding itself (buffer marshaling, message boundaries, NUL-safe payloads, the sentinel/error mappings, the selectable fd, lifecycle) — they don't re-test shr's semantics, which the C tests in ../../../tests cover.

Status

Covers create / open / read / write / close, the selectable fd, the core flags, and metrics (stat() wrapping shr_stat, farm_stat() wrapping shr_farm_stat). Not yet wrapped: the variadic shr_init/shr_open args (SHR_APPDATA_1 size, SHR_MAXMSGS_2 count), shr_readv/shr_writev, shr_flush, shr_ctl.