builtin
Attributes¶
GetBufContext = _GeneratorContextManager[tuple[ShmBlock, memoryview], None, None]
module-attribute
¶
ONE_MB = 1024 * 1024
module-attribute
¶
Classes¶
CodecRegistry
¶
Ordered registry of encoders used for IPC serialization.
Source code in brmspy/_session/codec/base.py
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 160 161 162 163 164 165 166 167 168 169 | |
Attributes¶
_by_codec = {}
instance-attribute
¶
_encoders = []
instance-attribute
¶
Functions¶
__init__()
¶
register(encoder)
¶
Register an encoder instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoder
|
Encoder
|
Encoder to register. Its |
required |
Source code in brmspy/_session/codec/base.py
encode(obj, shm_pool)
¶
Encode an object by selecting the first encoder that accepts it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Value to encode. |
required |
shm_pool
|
Any
|
SHM pool used by codecs for allocating buffers. |
required |
Returns:
| Type | Description |
|---|---|
EncodeResult
|
|
Source code in brmspy/_session/codec/base.py
decode(payload, shm_pool)
¶
Decode a payload using a named codec.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codec
|
str
|
Codec identifier previously returned by |
required |
meta
|
dict[str, Any]
|
Codec metadata. |
required |
buffers
|
list[memoryview]
|
Memoryviews for attached SHM buffers. |
required |
buffer_specs
|
list[dict]
|
Original buffer specs (name/size) corresponding to |
required |
shm_pool
|
Any
|
SHM pool (some codecs may attach additional buffers). |
required |
Returns:
| Type | Description |
|---|---|
Any
|
|
Source code in brmspy/_session/codec/base.py
_attach_shm_lifetime(obj, shms)
classmethod
¶
Keep SHM blocks alive as long as obj is alive.
Source code in brmspy/_session/codec/base.py
EncodeResult
dataclass
¶
Result of encoding a Python value for IPC transfer.
Attributes:
| Name | Type | Description |
|---|---|---|
codec |
str
|
Codec identifier. |
meta |
dict[str, Any]
|
JSON-serializable metadata required for decoding. |
buffers |
list[ShmRef]
|
Shared-memory blocks backing the encoded payload. |
Source code in brmspy/types/session.py
Encoder
¶
Bases: Protocol
Protocol implemented by codecs in the session codec registry.
Source code in brmspy/types/session.py
PayloadRef
¶
Bases: TypedDict
Encoded argument/result payload sent over the control pipe.
A payload is:
codec: the codec identifier used by the registrymeta: JSON-serializable metadata needed to reconstruct the valuebuffers: shared-memory buffer references backing the payload
Source code in brmspy/types/session.py
ShmSeriesMetadata
¶
Bases: TypedDict
Source code in brmspy/types/shm_extensions.py
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.
Source code in brmspy/types/shm_extensions.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
Attributes¶
_shm_metadata
instance-attribute
¶
Functions¶
from_metadata(meta, block)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
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. |
Source code in brmspy/types/shm_extensions.py
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).
Source code in brmspy/types/shm_extensions.py
is_string_object(a, sample=1000)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
to_shm(obj, shm_pool)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
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 |
Source code in brmspy/types/shm_extensions.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
Attributes¶
_metadata = ['_shm_metadata']
class-attribute
instance-attribute
¶
_shm_metadata
instance-attribute
¶
_constructor
property
¶
Functions¶
_create_col_metadata(series, block, arr=None, **params)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
_set_col_raw(col, value)
¶
_set_shm_metadata(meta)
¶
_put_col_in_shm(df, col, shm_pool, replace=False)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
_reconstruct_series(meta, block, nrows, index)
classmethod
¶
Source code in brmspy/types/shm_extensions.py
__setitem__(key, value)
¶
_on_column_added(col)
¶
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. |
Source code in brmspy/types/shm_extensions.py
Attributes¶
_metadata = ['_shm_metadata']
class-attribute
instance-attribute
¶
_shm_metadata
instance-attribute
¶
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
|
|
Source code in brmspy/types/shm_extensions.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
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
NumpyArrayCodec
¶
Bases: Encoder
SHM-backed codec for numpy.ndarray.
Source code in brmspy/_session/codec/builtin.py
PandasDFCodec
¶
Bases: Encoder
SHM-backed codec for numeric-only pandas.DataFrame.
Object-dtype columns are intentionally rejected to avoid surprising implicit conversions; those cases fall back to pickle.
Source code in brmspy/_session/codec/builtin.py
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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
Functions¶
can_encode(obj)
¶
encode(obj, shm_pool)
¶
Source code in brmspy/_session/codec/builtin.py
decode(payload, get_buf, *args)
¶
Source code in brmspy/_session/codec/builtin.py
PickleCodec
¶
Bases: Encoder
Pickle fallback codec (internal).
Always encodes successfully, so it must be registered last. The pickled bytes are still stored in SHM to keep pipe traffic small and bounded.
Source code in brmspy/_session/codec/builtin.py
Functions¶
can_encode(obj)
¶
encode(obj, shm_pool)
¶
Source code in brmspy/_session/codec/builtin.py
decode(payload, get_buf, *args)
¶
Source code in brmspy/_session/codec/builtin.py
InferenceDataCodec
¶
Bases: Encoder
Encode arviz.InferenceData by pushing its underlying arrays into shm.
Source code in brmspy/_session/codec/builtin.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
Functions¶
can_encode(obj)
¶
encode(obj, shm_pool)
¶
Source code in brmspy/_session/codec/builtin.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
decode(payload, get_buf, *args)
¶
Source code in brmspy/_session/codec/builtin.py
GenericDataClassCodec
¶
Bases: Encoder
Generic codec for dataclasses (internal).
Encodes each init=True field by delegating to a
CodecRegistry. Use skip_fields to exclude
fields that must not cross the boundary.
Source code in brmspy/_session/codec/builtin.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
Attributes¶
_cls = cls
instance-attribute
¶
_registry = registry
instance-attribute
¶
codec = f'dataclass::{cls.__module__}.{cls.__qualname__}'
instance-attribute
¶
_skip_fields = skip_fields or set()
instance-attribute
¶
_field_names = []
instance-attribute
¶
Functions¶
__init__(cls, registry, *, skip_fields=None)
¶
Source code in brmspy/_session/codec/builtin.py
can_encode(obj)
¶
encode(obj, shm_pool)
¶
Source code in brmspy/_session/codec/builtin.py
decode(payload, get_buf, *args)
¶
Source code in brmspy/_session/codec/builtin.py
Functions¶
log_warning(msg, method_name=None)
¶
Log a warning message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
msg
|
str
|
The warning message to log |
required |
method_name
|
str
|
The name of the method/function. If None, will auto-detect from call stack. |
None
|