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. |
offset |
int
|
Byte offset within the SHM block (for slab sub-allocations). |
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 with slab allocation.
When a slab is active (via open_slab/seal_slab), alloc() bump-allocates
sub-regions from a single large SharedMemory block, drastically reducing the
number of file descriptors for workloads that encode many small arrays
(e.g. InferenceData with hundreds of data variables).
_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).
Source code in brmspy/_session/transport.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | |
Attributes¶
_ALIGN = 64
class-attribute
instance-attribute
¶
_manager = manager
instance-attribute
¶
_blocks = {}
instance-attribute
¶
_slab = None
instance-attribute
¶
_slab_offset = 0
instance-attribute
¶
_slab_capacity = 0
instance-attribute
¶
Functions¶
__init__(manager)
¶
open_slab(capacity)
¶
Pre-allocate a slab of at least capacity bytes.
While a slab is open, alloc() bump-allocates from it instead of
creating individual SharedMemory segments. Call seal_slab() when
the batch of allocations is complete.