Skip to content

PyPI PyPI - Python Version License CI uv Ruff Checked with mypy Conventional Commits

sharedbox

sharedbox lets Python processes on one machine share data without a server and without pickling. A box is one record that every process reads and writes, as if they all held the same dataclass, and a stream is a flow of typed items that one process sends and each reader receives once. You declare the fields once, and every process that opens the box or the stream works on the same bytes. The bytes sit in shared memory, a block of memory the operating system lets several processes use at once, so nothing passes through a pipe or a socket between the processes.

A first look

Here a parent process creates a mailbox, starts a carrier process that drops a letter in, and prints the change the moment it happens:

import multiprocessing as mp
from typing import Annotated

from sharedbox import Capacity, SharedBox


class Mailbox(SharedBox):
    letters: int
    flag_up: bool
    owner: Annotated[str, Capacity(32)]


def deliver() -> None:
    mailbox = Mailbox.attach()  # finds the box by its class
    mailbox.letters = 1
    mailbox.close()


if __name__ == "__main__":
    with Mailbox(0, False, "Ada") as mailbox:
        mailbox.events.letters.connect(lambda new, old: print(old, "->", new))
        carrier = mp.Process(target=deliver)
        carrier.start()
        carrier.join()  # prints: 0 -> 1
    Mailbox.unlink()

Capacity(32) gives the text field room for 32 bytes, since every field has a fixed size. Running the script prints:

0 -> 1

Step through what happens, and point at a shape to read more:

One box, two processes

The parent creates the box and connects a callback to letters.

parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.1 -> with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1 -> with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.
parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.1 -> with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1 -> with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.

The carrier starts and attaches to the box. Nobody hands it the box: Mailbox.attach() works out its name from the class.

parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1 -> mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1 -> mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.
parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1 -> mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1 -> mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.

The carrier writes letters. The new value is in shared memory at once, for every process.

parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    mailbox = Mailbox.attach() 2 -> mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2 -> mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.
parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2        mailbox.events.letters.connect(lambda new, old: ...) 3 ->     carrier.start()1    mailbox = Mailbox.attach() 2 -> mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2 -> mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.

The parent's watcher thread wakes on the write and calls the callback, which prints 0 -> 1.

parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2 ->     mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2 ->     mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.
parent processCreates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters.the boxin shared memoryOne named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes.carrier processRuns deliver() in a separate process.1    with Mailbox(0, False, "Ada") as mailbox: 2 ->     mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    with Mailbox(0, False, "Ada") as mailbox: 2 ->     mailbox.events.letters.connect(lambda new, old: ...) 3        carrier.start()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close()1    mailbox = Mailbox.attach() 2    mailbox.letters = 1 3    mailbox.close() createsattaches by classletters = 1prints 0 -> 1Creates the box with Mailbox(0, False, "Ada") and connects a callback to events.letters. One named block of shared memory holding letters, flag_up and owner. Every process that opens it reads and writes the same bytes. Runs deliver() in a separate process.

Is it for you?

sharedbox fits when separate processes on one machine need the same data at once: web workers sharing state with a dashboard, a GUI and the backend process that does its work, the stages of a data pipeline, the state of a game or a simulation, or lab instruments and the programs that show them. Reading or writing a number or a short string takes well under a microsecond, and a process that is reading never holds up one that is writing.

If your code runs in threads of one process instead, you can share ordinary Python objects, which is simpler; When to use sharedbox compares the two.

sharedbox runs on CPython 3.11 or newer, on Windows and Linux; How to install sharedbox has the details.

Where to go next

  • Tutorials: start here if sharedbox is new to you, and build a small script one step at a time.
  • How-to Guides: install sharedbox, then get one task done, such as storing an array or naming a box.
  • Explanations: how a box works inside, and why it is built that way.
  • Reference: the API, the memory layout and the C and C++ interface.
  • Changelog: what changed in each release.
  • Contributing: set up a copy you can change, run the tests and send your change.