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.
Here a parent process creates a mailbox, starts a carrier process that
drops a letter in, and prints the change the moment it happens:
importmultiprocessingasmpfromtypingimportAnnotatedfromsharedboximportCapacity,SharedBoxclassMailbox(SharedBox):letters:intflag_up:boolowner:Annotated[str,Capacity(32)]defdeliver()->None:mailbox=Mailbox.attach()# finds the box by its classmailbox.letters=1mailbox.close()if__name__=="__main__":withMailbox(0,False,"Ada")asmailbox:mailbox.events.letters.connect(lambdanew,old:print(old,"->",new))carrier=mp.Process(target=deliver)carrier.start()carrier.join()# prints: 0 -> 1Mailbox.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->withMailbox(0,False,"Ada")asmailbox:2mailbox.events.letters.connect(lambdanew,old:...)3carrier.start()1 mailbox = Mailbox.attach()2 mailbox.letters =13 mailbox.close()1mailbox=Mailbox.attach()2mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2 mailbox.events.letters.connect(lambda new, old:...)3-> carrier.start()1withMailbox(0,False,"Ada")asmailbox:2mailbox.events.letters.connect(lambdanew,old:...)3->carrier.start()1-> mailbox = Mailbox.attach()2 mailbox.letters =13 mailbox.close()1->mailbox=Mailbox.attach()2mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2 mailbox.events.letters.connect(lambda new, old:...)3-> carrier.start()1withMailbox(0,False,"Ada")asmailbox:2mailbox.events.letters.connect(lambdanew,old:...)3->carrier.start()1-> mailbox = Mailbox.attach()2 mailbox.letters =13 mailbox.close()1->mailbox=Mailbox.attach()2mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2 mailbox.events.letters.connect(lambda new, old:...)3-> carrier.start()1withMailbox(0,False,"Ada")asmailbox:2mailbox.events.letters.connect(lambdanew,old:...)3->carrier.start()1 mailbox = Mailbox.attach()2-> mailbox.letters =13 mailbox.close()1mailbox=Mailbox.attach()2->mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2 mailbox.events.letters.connect(lambda new, old:...)3-> carrier.start()1withMailbox(0,False,"Ada")asmailbox:2mailbox.events.letters.connect(lambdanew,old:...)3->carrier.start()1 mailbox = Mailbox.attach()2-> mailbox.letters =13 mailbox.close()1mailbox=Mailbox.attach()2->mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2-> mailbox.events.letters.connect(lambda new, old:...)3 carrier.start()1withMailbox(0,False,"Ada")asmailbox:2->mailbox.events.letters.connect(lambdanew,old:...)3carrier.start()1 mailbox = Mailbox.attach()2 mailbox.letters =13 mailbox.close()1mailbox=Mailbox.attach()2mailbox.letters=13mailbox.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.1with Mailbox(0,False,"Ada")as mailbox:2-> mailbox.events.letters.connect(lambda new, old:...)3 carrier.start()1withMailbox(0,False,"Ada")asmailbox:2->mailbox.events.letters.connect(lambdanew,old:...)3carrier.start()1 mailbox = Mailbox.attach()2 mailbox.letters =13 mailbox.close()1mailbox=Mailbox.attach()2mailbox.letters=13mailbox.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.
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.