spdl.pipeline.SharedMemoryRingBuffer¶
- class SharedMemoryRingBuffer(capacity: int, *, acquire_timeout: float = 60.0, pin_memory: bool = False, _mp_context: BaseContext | None = None)[source]¶
[Experimental] A single shared-memory ring buffer for cross-process payloads.
Construct it in the parent process and pass it to
spdl.pipeline.iterate_in_subprocess()via thearenaargument; ownership transfers to that call, which closes and unlinks it at teardown.Added in version 0.5.0.
Changed in version 0.6.0: The producer now blocks on a process-shared
multiprocessing.Conditionwhen the arena is full and resumes once the consumer releases enough space, throttling the producer instead of failing it. Passacquire_timeout=0to restore the legacy raise-immediately behavior.Added in version 0.6.0: The
acquire_timeoutargument.See also
benchmark_arena_transportBenchmarks this ring buffer against a plain pickle/queue transfer and the segment pool, across payload types and sizes.
benchmark_pinned_arenaMatched public pool/ring
pin_memory=False/Truecomparisons through subprocess iteration and synchronized H2D, with archival earlier results.- Shared-memory arena transport benchmark
Case study with the measured CPU, throughput, and memory numbers.
- Parameters:
capacity – Size of the payload arena in bytes. Must be at least as large as the biggest single pipeline unit (the sum of the binaries offloaded from one item). Size it to the in-flight high-water mark, roughly
(buffer_size + 2) * max_unit_bytes.acquire_timeout – Seconds
write_binarywaits for the consumer to free enough space before raisingBufferError. Guards against a stalled/dead consumer; set to0for non-blocking behavior (raise immediately when the in-progress unit would not fit).pin_memory – Copy supported restored tensor/array/memoryview payloads directly into independent Torch-allocator pinned destinations on the CUDA device selected by
LOCAL_RANK. The source ring is not CUDA-registered and its existing copy-out/reclaim contract is unchanged. CUDA initializes lazily only in the consumer. Retained outputs can accumulate pinned memory beyond ring capacity. Rebuilt owning bytes/bytearrays and Packets may be pageable. Defaults toFalse.
Changed in version 0.9.0: Public endpoint methods conform to
ArenaProtocol. Storage sizing, counters and lifecycle helpers used only by SPDL are private; constructor configuration remains public. Native exports retain storage independently of the reader anchor list.Added in version 0.9.0: The
pin_memoryargument.Methods
close()Close this handle; only the original creator cancels shared arena work.
Return the reader endpoint (call in the parent process).
Return the writer endpoint (call in the worker process).
Request shutdown and wake a producer blocked on arena capacity.
unlink()Remove creator names only after physical retirement completes.
Attributes
Name of the underlying shared-memory segment.
- close() → None[source]¶
Close this handle; only the original creator cancels shared arena work.
Creator close precedes pipeline stop/join to wake blocked work. After actual worker reap, retry retirement and then unlink. Deserialized handles only close their own mappings and cannot unlink creator names. Independent private copies and pinned H2D outputs remain valid. A normal return may mean deferred physical retirement;
unlink()reportsBufferErroruntil the retained mapping is closed.- Raises:
RuntimeError – Bounded control acquisition failed or the handle was inherited across fork. Owned mappings remain retained; do not interpret an unsuccessful close as proof of safe retirement.
Changed in version 0.9.0: Creator close cancels and defers unsafe retirement; deserialized holder close does not change the shared terminal state.
- shutdown_arena() → None[source]¶
Request shutdown and wake a producer blocked on arena capacity.
Call before worker join; this requests shutdown without closing mappings, unregistering live storage or unlinking names. Existing exports remain valid. Complete physical retirement separately through
close()once actual worker and storage quiescence is established.- Raises:
RuntimeError – Control state is unavailable or retired. Owned backing remains retained; this failure is not proof of safe retirement.
- unlink() → None[source]¶
Remove creator names only after physical retirement completes.
- Raises:
BufferError – The mapping is retained. Confirm worker/owner quiescence and retry
close()before removing names.RuntimeError – This is not the original creator handle.