transport
Shared-memory transport utilities (internal).
RModuleSession uses shared memory to move large payloads between main and worker.
The parent allocates blocks and passes only (name, size) references over the Pipe.
The worker (or the main process during decode) attaches by name to access buffers.
This module implements the concrete ShmPool used by the session layer.
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 |
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.
Source code in brmspy/types/shm.py
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.
Source code in brmspy/types/shm.py
_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.
Source code in brmspy/types/shm.py
Functions¶
__init__(manager)
¶
Create a pool bound to an existing SharedMemoryManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manager
|
SharedMemoryManager
|
Manager used to allocate blocks. |
required |
Source code in brmspy/types/shm.py
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. |
Source code in brmspy/types/shm.py
attach(ref)
¶
Attach to an existing shared-memory block by name.
Returns:
| Type | Description |
|---|---|
ShmBlock
|
Attached block. |
close_all()
¶
ShmPool
¶
Bases: ShmPool
Concrete shared-memory pool implementation that temporarily tracks attached blocks.
_blocks dict keeps references to shm buffers TEMPORARILY and is cleaned up before each 'responding to main' or 'sending new message to worker'. This allows the in-between processing of shm buffers to rely on the buffers not being garbage collected.
After reconstructing an object from a shm buffer, it's the CodecRegistrys role to take over the reference by initiating a weakref between the reconstructed object and buffer (or skipping if the object is temporary).
This helps ensure that a minimal amount of shm buffers are actively mapped and garbage collection can remove file descriptors no longer needed.