Referring to another box¶
A box can point at another box, the way an attribute of one
Python object can hold another object. In this tutorial you make a
Carrier whose current stop is a Mailbox, connect a callback that hears
about changes inside whichever mailbox the carrier stops at, and then move
the carrier on to a second mailbox.
Before you start¶
What you need
The script mailbox.py from Reacting to changes.
1. Define the carrier¶
Add this class below react:
The stop field doesn't copy a mailbox's values into the carrier. It is a
reference field: it stores which Mailbox the carrier
points at, and the mailbox keeps its values in its own shared memory. The
| None lets the field be empty, which is also its default here.
Reference fields explains what exactly the
field stores.
2. Follow the mailbox¶
Add this function below Carrier:
def follow() -> None:
with (
Mailbox.create("tutorial-north") as north,
Mailbox.create("tutorial-south") as south,
Carrier.create("tutorial-carrier", north) as carrier,
):
seen: queue.Queue[int] = queue.Queue()
carrier.events.follow("stop").letters.connect(lambda new, old: seen.put(new))
north.letters = 1
print("letters at the stop", seen.get(timeout=5))
carrier.stop = south
# forwarding moves to the new box on a background thread, shortly after
time.sleep(0.5)
south.letters = 7
print("letters at the stop", seen.get(timeout=5))
Carrier.unlink("tutorial-carrier")
Mailbox.unlink("tutorial-north")
Mailbox.unlink("tutorial-south")
The function creates two mailboxes, north and south, and a carrier
whose stop is north. events.follow("stop")
gives you signals like a mailbox's own events, and each one fires when the
mailbox that carrier.stop currently points at changes. The callback runs on a
background thread, and printing from two threads at once can mix their
lines, so the callback only puts each new count on a queue. The main
thread takes the counts from there and prints them.
Assigning south to carrier.stop makes the callback follow south
instead of north.
The switch to a new box takes a moment
After you assign south to carrier.stop, a background thread moves
the following from north to south, shortly after the assignment
returns. A write to south made before that is missed by the callback.
That is why the script waits half a second before it writes to south;
in your own code,
leave the same gap or don't rely on that first write.
3. Run it¶
In the if block at the end of the file, add the highlighted line:
Run the script:
The last two lines are new: the callback heard the write to north, then
the write to south:
What you built¶
You have a carrier that points at a mailbox, and a callback that keeps hearing about the carrier's stop even after you move the carrier on to a different one.
Next steps¶
- How to follow a whole reference graph
shows how to follow every box the carrier reaches, not just one field, by
calling
followwithout a field name. - Reference fields explains what a reference stores and how it can break.
- Limits says what following costs: one thread for each box you follow.
The whole script
"""The script the sharedbox tutorial builds, one page at a time."""
import multiprocessing as mp
import queue
import time
from typing import Annotated
from sharedbox import Capacity, SharedBox
class Mailbox(SharedBox):
letters: int = 0
flag_up: bool = False
owner: Annotated[str, Capacity(32)] = ""
def deliver(letters: int) -> None:
mailbox = Mailbox.attach("tutorial-mailbox")
mailbox.letters = letters
mailbox.close()
def share() -> None:
with Mailbox.create("tutorial-mailbox", owner="Ada") as mailbox:
carrier = mp.Process(target=deliver, args=(2,))
carrier.start()
carrier.join()
print("letters left by the carrier:", mailbox.letters)
Mailbox.unlink("tutorial-mailbox")
def react() -> None:
with Mailbox.create("tutorial-mailbox", owner="Ada") as mailbox:
mailbox.events.letters.connect(
lambda new, old: print(f"letters {old} -> {new}")
)
letters = mailbox.watch("letters")
carrier = mp.Process(target=deliver, args=(3,))
carrier.start()
watched = next(iter(letters))
carrier.join()
print("watched", watched)
Mailbox.unlink("tutorial-mailbox")
class Carrier(SharedBox):
stop: Mailbox | None = None
def follow() -> None:
with (
Mailbox.create("tutorial-north") as north,
Mailbox.create("tutorial-south") as south,
Carrier.create("tutorial-carrier", north) as carrier,
):
seen: queue.Queue[int] = queue.Queue()
carrier.events.follow("stop").letters.connect(lambda new, old: seen.put(new))
north.letters = 1
print("letters at the stop", seen.get(timeout=5))
carrier.stop = south
# forwarding moves to the new box on a background thread, shortly after
time.sleep(0.5)
south.letters = 7
print("letters at the stop", seen.get(timeout=5))
Carrier.unlink("tutorial-carrier")
Mailbox.unlink("tutorial-north")
Mailbox.unlink("tutorial-south")
if __name__ == "__main__":
share()
react()
follow()