GitHub

shared_memory

API reference for shared_memory.

Typed arrays in shared memory, for passing data to workers without copying (Effect's SharedMemory).

make(kind, length) allocates a zero-filled SharedArray in a multiprocessing.shared_memory segment, and schema(kind) is the payload field that carries it in a worker message. Unlike a copied message, every worker that gets the array reads and writes the same memory as the sender.

Both Python platforms share it. A thread worker gets the array itself. A process worker's message carries only the segment's name, and the worker maps the segment when the message arrives, so its writes land in the sender's array. (In Effect a child process gets a copy, so its schema rejects it there.)

  • The scope that ran make frees the segment when it closes, however it closes. A worker's mapping never frees it. If the process that made it dies first, Python's resource tracker frees it.
  • Workers writing the same elements race: give each its own range.
  • A field declared as S.Bytes or a list is copied, and a worker's writes to it are lost. Use schema for shared fields.

Values

AttributeTypeDescription
int8Kind[int]
uint8Kind[int]
int16Kind[int]
uint16Kind[int]
int32Kind[int]
uint32Kind[int]
int64Kind[int]
uint64Kind[int]
float32Kind[float]
float64Kind[float]

Classes

Functions

is_available

is_available() -> bool

Whether this platform has shared memory (all but Emscripten and WASI).

Returns bool

def is_available() -> bool:
    """Whether this platform has shared memory (all but Emscripten and WASI)."""
    return _segments() is not None

make

make[T: int | float](kind, length) -> Effect[SharedArray[T], SharedMemoryError, Scope]

A zero-filled array of length kind elements in shared memory, freed when the scope closes. Fails with SharedMemoryError when the platform has no shared memory or refuses a segment.

ParameterTypeDescription
kindKind[T]
lengthint

Returns Effect[SharedArray[T], SharedMemoryError, Scope]

def make[T: int | float](
    kind: Kind[T], length: int
) -> Effect[SharedArray[T], SharedMemoryError, Scope]:
    """A zero-filled array of ``length`` ``kind`` elements in shared memory,
    freed when the scope closes. Fails with ``SharedMemoryError`` when the
    platform has no shared memory or refuses a segment."""
    if length < 0:
        raise ValueError(f"a shared array's length can't be negative, got {length}")
    opened = acquire_release(
        suspend(lambda: _allocate(kind, length)), lambda opened, _: sync(opened.free)
    )
    return opened.map(lambda opened: opened.shared)

is_shared

is_shared(value) -> TypeIs[SharedArray[Any]]

Whether value is an array in shared memory.

ParameterTypeDescription
valueobject

Returns TypeIs[SharedArray[Any]]

def is_shared(value: object) -> TypeIs[SharedArray[Any]]:
    """Whether ``value`` is an array in shared memory."""
    return isinstance(value, SharedArray)

schema

schema[T: int | float](kind) -> S.Bottom[SharedArray[T]]

A worker message field holding a kind array in shared memory. It accepts only a SharedArray of kind, so a copy fails rather than losing the worker's writes, and S.to_codec_json passes the array through unchanged, as worker codecs need.

ParameterTypeDescription
kindKind[T]

Returns S.Bottom[SharedArray[T]]

def schema[T: int | float](kind: Kind[T]) -> S.Bottom[SharedArray[T]]:
    """A worker message field holding a ``kind`` array in shared memory. It
    accepts only a ``SharedArray`` of ``kind``, so a copy fails rather than
    losing the worker's writes, and ``S.to_codec_json`` passes the array
    through unchanged, as worker codecs need."""

    def guard(value: object) -> TypeIs[SharedArray[T]]:
        return isinstance(value, SharedArray) and cast("SharedArray[Any]", value).kind == kind

    return S.declare(
        guard, expected=f"{kind.name} array in shared memory", to_codec_json=passthrough_link
    )