Skip to content

shm

SHM-backed NumPy and pandas helpers.

These types are thin wrappers around NumPy/pandas objects that keep a reference to the shared-memory block that backs the underlying data. They enable brmspy's codecs to avoid extra copies when transporting large numeric payloads between the main process and the worker.

See Also

brmspy._session.codec.builtin.NumpyArrayCodec Encodes/decodes NumPy arrays into shared memory. brmspy._session.codec.builtin.PandasDFCodec Encodes/decodes DataFrames into shared memory. brmspy.types.shm Base shared-memory block and pool types.

Classes

ShmArray

Bases: ndarray

NumPy array view backed by a shared-memory block.

Attributes:

Name Type Description
block ShmRef

Reference to the shared-memory block backing the array data.

Notes

This is a view over SharedMemory.buf. Closing/unlinking the underlying shared memory while the array is still in use will lead to undefined behavior.

Functions

from_block(block, shape, dtype, **kwargs) classmethod

Create an array view backed by an existing shared-memory block.

Parameters:

Name Type Description Default
block ShmBlock

Attached shared-memory block.

required
shape tuple[int, ...]

Desired array shape.

required
dtype dtype

NumPy dtype of the array.

required
**kwargs

Reserved for future compatibility. Currently unused.

{}

Returns:

Type Description
ShmArray

Array view into the shared-memory buffer.

array_order(a) classmethod

Determine how an array can be reconstructed from a raw buffer.

Returns "C" for C-contiguous arrays, "F" for Fortran-contiguous arrays, otherwise "non-contiguous" (meaning: bytes were obtained by forcing a contiguous copy during encoding).

ShmDataFrameSimple

Bases: DataFrame

pandas DataFrame backed by a single shared-memory block (numeric only).

Attributes:

Name Type Description
block ShmRef

Reference to the shared-memory block backing the DataFrame's values.

Functions

from_block(block, nrows, ncols, columns, index, dtype) classmethod

Construct a DataFrame backed by a single SHM block.

Parameters:

Name Type Description Default
block ShmBlock

Attached shared-memory block containing a contiguous 2D numeric matrix.

required
nrows int

DataFrame shape.

required
ncols int

DataFrame shape.

required
columns list[Any] or None

Column/index labels.

required
index list[Any] or None

Column/index labels.

required
dtype str or dtype

Dtype of the matrix stored in the block.

required

Returns:

Type Description
ShmDataFrameSimple

ShmDataFrameColumns

Bases: DataFrame

pandas DataFrame backed by per-column shared-memory blocks (numeric only).

Attributes:

Name Type Description
_blocks_columns dict[str, PandasColumnMetadata]

Mapping from column name to data required for its reconstruction

Classes

ShmRef

Bases: TypedDict

Reference to a shared-memory block sent over IPC.

Attributes:

Name Type Description
name str

Shared memory block name (as assigned by SharedMemoryManager).

size int

Allocated block size in bytes.

content_size int

Actual used size

temporary bool

Whether this buffer can be GC-d immediately after use or should it be attached to object its constructed into.

Notes

Codecs may store a logical payload smaller than size. In that case, the codec metadata must include the logical nbytes/length so that decoders can slice the buffer appropriately.

ShmBlock dataclass

Attached shared-memory block (name/size + live SharedMemory handle).

Notes

This object owns a SharedMemory handle and must be closed when no longer needed. In brmspy this is managed by a ShmPool implementation.

ShmPool

Minimal interface for allocating and attaching shared-memory blocks.

The concrete implementation lives in brmspy._session.transport.ShmPool and tracks blocks so they can be closed on teardown.

Functions

__init__(manager)

Create a pool bound to an existing SharedMemoryManager.

Parameters:

Name Type Description Default
manager SharedMemoryManager

Manager used to allocate blocks.

required
alloc(size, temporary=False)

Allocate a new shared-memory block.

Parameters:

Name Type Description Default
size int

Size in bytes.

required

Returns:

Type Description
ShmBlock

Newly allocated block.

attach(ref)

Attach to an existing shared-memory block by name.

Returns:

Type Description
ShmBlock

Attached block.

close_all()

Close all tracked shared-memory handles owned by this pool.

Returns:

Type Description
None