Skip to main content

shmobj

English | Русский

Arbitrarily nested dict/list-like structures in shared memory (via mmap), accessible from multiple processes without copying or deserialization.

shmobj provides the FrozenDict (Mapping) and FrozenList (Sequence) proxy objects for accessing data in shared memory. The data is stored in memory only once. All processes access the same shared structure, and Python objects are created only for the values that are actually accessed.

Why shmobj?

CPU-bound Python workloads often use multiple processes to work around the GIL. Each process would normally keep its own copy of the data. For example, a 100 MB lookup table, configuration, or metrics dataset shared across 8 workers would consume about 800 MB of memory. shmobj keeps a single shared copy instead.

Benchmark: one million records with a structure like {"id", "name", "score", "tags": [...], "ok", "x"}. Each process iterated over every record.

Processes pickle — total memory usage shmobj — total memory usage
1 628 MB 168 MB
4 2.5 GB 168 MB
8 5.0 GB 168 MB

More measurements: doc/status.md.

Attaching to an existing segment takes about 50 µs, compared with about 1.5 s for pickle.loads on the same dataset. The trade-off is that key lookups are 3–6 times slower than in a regular dict. get_many(), iter_values(), iter_items() and materialize() help speed up sequential reads and repeated access.

More on performance and access patterns: Access speed.

The detailed documentation in doc/ is currently available in Russian.

Installation

pip install shmobj

Prebuilt wheels are available for:

  • Python: CPython 3.11 and later;
  • Linux: x86_64, aarch64;
  • Windows: x64;
  • macOS: arm64, x86_64 — partial support (limitations).

On other platforms, pip builds the package from the source distribution (sdist), which requires Rust (building from source).

The same wheels are available on the releases page:

pip install ./shmobj-<version>-cp311-abi3-<platform>.whl

Main use cases

Use case API Example
Share one dataset across multiple processes create() / attach() shared_pool.py
Let readers access the latest published snapshot shmobj.latest latest_pubsub.py
Receive new snapshots without polling shmobj.channel channel_pubsub.py
Read data from a file or bytes dumps() / loads() file_mmap.py

All examples are in the examples directory. Install shmobj before running them.

Shared dataset

create() creates a segment and writes the data structure to it. Other processes attach to the segment by name using attach(). The memory is released after unlink() has been called and all processes have closed the segment (on Windows, after the last handle is closed).

import shmobj

# Writer process

segment = shmobj.create({
    "users": [
        {"id": 1, "name": "Ann", "score": 42},
        {"id": 2, "name": "Bob", "score": 37},
    ]
})

print(segment.name)  # pass the name to the reader processes

# Reader process
with shmobj.attach(segment.name) as segment:
    users = segment.root["users"]

    print(users[0].get_many(["id", "name"]))
    total = sum(user["score"] for user in users.iter_values())

FrozenDict and FrozenList implement the Mapping and Sequence interfaces, so you can use them much like regular dict and list objects.

It also works with multiprocessing (shared_pool.py):

import multiprocessing
import shmobj


def init(segment_name):
    global users
    users = shmobj.attach(segment_name).root["users"]  # The proxy keeps the segment open for the lifetime of the worker.


def score(index):
    return users[index]["score"]


if __name__ == "__main__":
    data = {"users": [{"id": i, "score": i % 100} for i in range(100_000)]}
    # Exiting the with block closes the segment and unlinks it from the OS.
    with shmobj.create(data, unlink_on_close=True) as segment:
        with multiprocessing.Pool(4, initializer=init, initargs=(segment.name,)) as pool:
            print(sum(pool.map(score, range(len(data["users"])), chunksize=10_000)))

shmobj.latest

shmobj.latest lets you publish snapshots under a given name. The writer publishes a new snapshot in place of the previous one, and readers get the current version by calling get(). No sockets are involved, and processes can start in any order.

from shmobj import latest

# Writer process
writer = latest.Writer("metrics")
writer.put({"vms": {101: {"cpu": 0.5}}})

# Reader process
reader = latest.Reader("metrics")
root = reader.get()  # None if no snapshot has been published yet; otherwise, the same object until it changes

As long as the snapshot remains unchanged, get() returns the same object. A reader only detects an update when it calls get() again.

Example: latest_pubsub.py.

Cleanup after a crash. On Linux and macOS, segment names may remain registered with the OS if the writer crashes. On Linux, these segments are located in /dev/shm. They remain there until a reboot, a container restart, or the next writer that uses the same name.

Pub/Sub: shmobj.channel

One writer, multiple readers. Readers are notified of new snapshots without polling.

  • A new subscriber immediately receives the latest snapshot.
  • Intermediate snapshots may be skipped: readers get the current state, not a queue of every update.
  • After all processes exit, the OS releases the channel resources; no names are left behind in /dev/shm either.
  • The channel address remains stable, so readers can reconnect after the writer restarts.

On Linux, an address such as "\0name" uses the abstract Unix socket namespace. It is not a file, and the kernel releases the address when the writer exits. Windows uses TCP loopback instead.

Synchronous API

import itertools
import sys
import time

from shmobj import channel

ADDRESS = ("127.0.0.1", 50555) if sys.platform == "win32" else "\0shmobj-example"

# Writer
with channel.Publisher() as pub:
    pub.accept_in_thread(channel.listen(ADDRESS))  # a background thread accepts subscribers
    for tick in itertools.count():
        pub.publish({"tick": tick, "vms": {101: {"cpu": tick % 100}}})  # non-blocking
        time.sleep(1)

# Reader:
while True:
    # wait until the writer starts listening; wait_writer=30 — at most 30 seconds
    with channel.Subscriber(channel.connect(ADDRESS, wait_writer=True)) as sub:
        while sub.wait():  # True — a snapshot arrived; False — the writer closed or crashed
            print(sub.latest["vms"][101]["cpu"])  # FrozenDict backed by shared memory

Example: channel_pubsub.py.

asyncio API

The channel supports asynchronous connections and waiting for updates. Waiting for the writer does not block the event loop.

import asyncio
import itertools
import sys

from shmobj import channel

ADDRESS = ("127.0.0.1", 50555) if sys.platform == "win32" else "\0shmobj-example"


# Writer
async def main():
    with channel.Publisher() as pub:
        pub.accept_in_loop(channel.listen(ADDRESS))  # the event loop accepts subscribers, without a separate thread
        for tick in itertools.count():
            pub.publish({"tick": tick, "vms": {101: {"cpu": tick % 100}}})
            await asyncio.sleep(1)


# Reader:
async def main():
    while True:
        sock = await channel.connect_async(ADDRESS, wait_writer=True)  # waiting does not block the event loop
        with channel.Subscriber(sock) as sub:
            while await sub.wait_async():
                print(sub.latest["vms"][101]["cpu"])


# Reader with a callback: called in the event loop for each new snapshot
def on_snapshot(root):
    print(root["vms"][101]["cpu"])


async def main():
    sock = await channel.connect_async(ADDRESS, wait_writer=True)
    with channel.Subscriber(sock) as sub:
        sub.attach(on_snapshot)  # calls on_snapshot(sub.latest) for each new snapshot
        while await sub.wait_async():  # also works with a callback; False — the writer closed or crashed
            pass

Example: channel_pubsub_async.py.

Synchronous and asynchronous writers and readers are compatible. For example, an asyncio reader can connect to a synchronous writer.

Files, bytes and mmap

dumps() creates a buffer in the shmobj format, and loads() opens the structure directly from a supported buffer (bytes, mmap, or another supported type) without copying the underlying data. When multiple processes read the same file, the OS serves its pages from the shared file cache.

import mmap
import shmobj

data = {"users": [{"id": 1, "name": "Ann", "score": 42}]}

# Save the structure to a file.
with open("data.shmobj", "wb") as file:
    file.write(shmobj.dumps(data))

with open("data.shmobj", "rb") as file:  # in each process, file pages come from the shared OS cache
    root = shmobj.loads(mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ))

Segment lifecycle

  • close() closes the segment in the current process.
  • unlink() removes the segment's name from the OS. Exiting a with block calls close(). Automatic unlink() is performed only for segments created with unlink_on_close=True.
  • Proxy objects keep the segment open. Accessing them after close() raises SegmentClosedError.
  • Linux and macOS: a segment is a POSIX shared memory object. On Linux, it is represented by a file in /dev/shm and stored in RAM. The segment exists until unlink() is called, not merely until its creator exits.
  • Windows: the segment is removed after the last handle is closed.

If the entire process group crashes on Linux or macOS, the segment name remains registered with the OS until a reboot (or a container restart), or until you run rm /dev/shm/<name> on Linux.

More: Segment lifecycle.

Limitations

  • Read-only access. Published data cannot be modified in place. To update it, publish a new snapshot via shmobj.latest or shmobj.channel.
  • Supported values: dict, list, tuple (represented as a list when read), None, bool, arbitrary-precision int, float, str and bytes. Dictionary keys must be scalar values.
  • macOS — partial support: shmobj.channel is not available (use shmobj.latest instead). The package passes the test suite but has not been used in production projects.
  • Format validation: accessing corrupted data raises FormatError. validate() checks the entire buffer.
  • Docker: by default, each container has its own /dev/shm (64 MB) and its own network. You can work around this by configuring shared IPC and shm_size in Compose, or by using a channel over a socket file in a shared volume — Docker.

Why not use Python's standard tools?

  • multiprocessing.shared_memory. It provides a shared byte buffer, but has no built-in support for nested Python structures. You must serialize the structure (for example, with pickle), and each process then deserializes it into its own copy.
  • multiprocessing.Manager().dict(). The data is managed by a separate server process. Accessing a dictionary item requires an IPC request and serialization.
  • fork. By itself, it does not guarantee memory savings for Python objects: reference-count updates can trigger copy-on-write, causing memory pages to be copied between processes.

More: Comparison with alternatives.

Documentation

Building from source

Quick install from Git

Install directly from the repository (pip downloads maturin and builds the wheel; Rust is required):

pip install git+https://github.com/Aleksandrln/shmobj

Full build from a project checkout

Requires CPython 3.11+ and stable Rust (rustup). On Windows, you also need Visual Studio Build Tools with the C++ component. Install Rust on Linux and macOS with:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

To build a wheel from a local checkout, install maturin in a virtual environment (Debian and Ubuntu block pip install outside a virtual environment under PEP 668):

git clone https://github.com/Aleksandrln/shmobj
cd shmobj
python3 -m venv .venv
. .venv/bin/activate              # Windows: .venv\Scripts\activate
pip install maturin
maturin build --release -o dist   # dist/shmobj-<version>-cp311-abi3-<platform>.whl
maturin sdist -o dist             # source distribution

The wheel can be installed in any CPython 3.11+ environment on the same platform: pip install dist/shmobj-*.whl. Without --release, the build is about 10 times slower. cargo build --release builds only the library, not the Python package.

Wheel layout and portable builds (older glibc, Windows builds from Linux): doc/build.md.

License

Dual-licensed under MIT or Apache-2.0, at your option.

Author

Created by AleksandrLn with the help of Claude by Anthropic.

Metadata

Release files for shmobj 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for shmobj 0.1.3
File Size Uploaded Provenance Actions
shmobj-0.1.3.tar.gz 69.1 kB With provenance Details for shmobj-0.1.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for shmobj 0.1.3
File Actions
shmobj-0.1.3-cp311-abi3-win_amd64.whl CPython 3.11 abi3 Windows x86-64 Details for shmobj-0.1.3-cp311-abi3-win_amd64.whl
shmobj-0.1.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.11 abi3 Linux glibc 2.17+ x86-64 Details for shmobj-0.1.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
shmobj-0.1.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.11 abi3 Linux glibc 2.17+ ARM64 Details for shmobj-0.1.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
shmobj-0.1.3-cp311-abi3-macosx_11_0_arm64.whl CPython 3.11 abi3 macOS 11.0+ ARM64 Details for shmobj-0.1.3-cp311-abi3-macosx_11_0_arm64.whl
shmobj-0.1.3-cp311-abi3-macosx_10_12_x86_64.whl CPython 3.11 abi3 macOS 10.12+ x86-64 Details for shmobj-0.1.3-cp311-abi3-macosx_10_12_x86_64.whl

Total release size: 1.7 MB

Release files / shmobj-0.1.3.tar.gz

Download URL shmobj-0.1.3.tar.gz
Size 69.1 kB
Tags Source
SHA-256 checksum
How to use checksums
4382c2aa6e0e6c17836da7d1e4b9feec083631c40dccfc90f6c17087a12fae5d
BLAKE2b-256 checksum
How to use checksums
7cd6b932550932ab011f5a4fb0856c5f1c778e06fdb3cb53b6010fba13871c0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / shmobj-0.1.3-cp311-abi3-win_amd64.whl

Download URL shmobj-0.1.3-cp311-abi3-win_amd64.whl
Size 250.8 kB
Tags CPython 3.11 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
3664307f7806414ab67dc2644ce693b48dc0631d0abbcd0cce88929936cc01a9
BLAKE2b-256 checksum
How to use checksums
ac9b8d78d127de8f54dad16649cddedbf5af75afe1203cb609e1370be1bed994
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / shmobj-0.1.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL shmobj-0.1.3-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 360.7 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
1e1c3ecf2611877348ba6771432aa73ac782fc0665b89989958b781cb8c1df8f
BLAKE2b-256 checksum
How to use checksums
f460a822bb4608ca2b7f9b655cf8ff07b951a6d77338423c9e7ca32a7270b3ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / shmobj-0.1.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL shmobj-0.1.3-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 359.8 kB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
af8a4d6aad0d2d10c62d82cd452e51231c8c6876443b95481c7d3476b2d0f8e7
BLAKE2b-256 checksum
How to use checksums
ac09b5c4e9f47969a6e56d2fdccd770a7ec1ccb4f829dc2345ea3afcea0350a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / shmobj-0.1.3-cp311-abi3-macosx_11_0_arm64.whl

Download URL shmobj-0.1.3-cp311-abi3-macosx_11_0_arm64.whl
Size 340.2 kB
Tags CPython 3.11 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
13605d38128508199349e1ae1d026da8894f6ef2be8810a66ded0d289a03469e
BLAKE2b-256 checksum
How to use checksums
c73643fdd4b7789c42cf9d12d013a27e46c083d787c93b004c20d6a618c93360
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Release files / shmobj-0.1.3-cp311-abi3-macosx_10_12_x86_64.whl

Download URL shmobj-0.1.3-cp311-abi3-macosx_10_12_x86_64.whl
Size 347.0 kB
Tags CPython 3.11 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
c0a51b037a386dd4860b146466493eb014d042c9cb12f869a8f3bb6ad2986b6f
BLAKE2b-256 checksum
How to use checksums
9acad593a9e5c47ba56b0c024e844616c5ba8ec7be070b4f56924bece042c866
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

Security for shmobj 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for shmobj-0.1.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 10, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page