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
makefrees 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.Bytesor a list is copied, and a worker's writes to it are lost. Useschemafor shared fields.
Values#
| Attribute | Type | Description |
|---|---|---|
int8 | Kind[int] | |
uint8 | Kind[int] | |
int16 | Kind[int] | |
uint16 | Kind[int] | |
int32 | Kind[int] | |
uint32 | Kind[int] | |
int64 | Kind[int] | |
uint64 | Kind[int] | |
float32 | Kind[float] | |
float64 | Kind[float] |
Classes#
Functions#
is_available#
is_available() -> boolWhether 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 Nonemake#
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.
| Parameter | Type | Description |
|---|---|---|
kind | Kind[T] | |
length | int |
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.
| Parameter | Type | Description |
|---|---|---|
value | object |
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.
| Parameter | Type | Description |
|---|---|---|
kind | Kind[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
)