How to follow a whole reference graph¶
Boxes can point at boxes that point at further boxes: a post office at its carrier, the carrier at the bag of letters it carries. When a box's reference fields lead on like this, they form a graph. This guide shows how one callback can hear about a change anywhere in it.
Before you start¶
What you need
Classes with reference fields. This guide uses a post office that refers to a carrier, which refers to a bag:
import queue
from typing import Any
from sharedbox import SharedBox
class Bag(SharedBox, name="example-bag"):
letters: int = 0
class Carrier(SharedBox, name="example-carrier"):
parcels: int = 0
bag: Bag | None = None
class PostOffice(SharedBox, name="example-post-office"):
carrier: Carrier | None = None
Referring to another box follows a single reference field; here you follow all of them at once.
1. Follow every reference¶
The script creates a bag, a carrier that points at it and a post office
that points at the carrier. Call follow on
the post office's events without naming a field, then connect to the
nested signal:
changes: queue.Queue[tuple[tuple[str, ...], Any]] = queue.Queue()
office.events.follow()
office.events.nested.connect(lambda path, new, old: changes.put((path, new)))
bag.letters = 3
print(changes.get(timeout=5)) # (('carrier', 'bag', 'letters'), 3)
carrier.parcels = 7
print(changes.get(timeout=5)) # (('carrier', 'parcels'), 7)
Your callback gets (path, new, old), where path lists the field names
on the way from the post office to the field that changed. When any process points
a reference field at a different box, the following moves to the new box by
itself.
The callbacks run on background threads, one for each box you follow. Hand
the values over to your own thread, as the queue does here, or connect with
thread="main", as the docstring of events
describes.
2. Follow one path instead¶
If you only care about one box deep in the graph, call follow with a
field at each level instead. Each call gives you the signals of the class
that field points at:
office.events.unfollow()
counts: queue.Queue[int] = queue.Queue()
bag_events = office.events.follow("carrier").follow("bag")
bag_events.letters.connect(lambda new, old: counts.put(new))
bag.letters = 4
print(counts.get(timeout=5)) # 4
3. Stop following¶
unfollow with a field stops what follow
with that field started, and without a field it stops everything, as the
script does before step 2. Closing the box stops all following too.
Following isn't free: each box you follow takes one waiter slot and one thread. Limits says what that adds up to for a large graph.
The whole script
"""The script of the guide "How to follow a whole reference graph"."""
import queue
from typing import Any
from sharedbox import SharedBox
class Bag(SharedBox, name="example-bag"):
letters: int = 0
class Carrier(SharedBox, name="example-carrier"):
parcels: int = 0
bag: Bag | None = None
class PostOffice(SharedBox, name="example-post-office"):
carrier: Carrier | None = None
with Bag() as bag, Carrier(0, bag) as carrier, PostOffice(carrier) as office:
changes: queue.Queue[tuple[tuple[str, ...], Any]] = queue.Queue()
office.events.follow()
office.events.nested.connect(lambda path, new, old: changes.put((path, new)))
bag.letters = 3
print(changes.get(timeout=5)) # (('carrier', 'bag', 'letters'), 3)
carrier.parcels = 7
print(changes.get(timeout=5)) # (('carrier', 'parcels'), 7)
office.events.unfollow()
counts: queue.Queue[int] = queue.Queue()
bag_events = office.events.follow("carrier").follow("bag")
bag_events.letters.connect(lambda new, old: counts.put(new))
bag.letters = 4
print(counts.get(timeout=5)) # 4
PostOffice.unlink()
Carrier.unlink()
Bag.unlink()