tlo.notify module
A dead simple synchronous notification dispatcher.
Usage
# In the notifying class/module from tlo.notify import notifier
notifier.dispatch(“simulation.on_start”, data={“one”: 1, “two”: 2})
# In the listening class/module from tlo.notify import notifier
- def on_notification(data):
print(“Received notification:”, data)
notifier.add_listener(“simulation.on_start”, on_notification)
- class Notifier
Bases:
objectA simple synchronous notification dispatcher supporting listeners.
- add_listener(notification_key, listener)
Register a listener for a specific notification.
- Parameters:
notification_key – The identifier to listen for.
listener – A callable to be invoked when the notification is dispatched.
- remove_listener(notification_key, listener)
Remove a previously registered listener for a notification.
- Parameters:
notification_key – The identifier.
listener – The listener callable to remove.
- dispatch(notification_key, data=None)
Dispatch a notification to all registered listeners.
- Parameters:
notification_key – The identifier.
data – Optional data to pass to each listener.
- clear_listeners()
Clear all registered listeners. Essential because the notifier is a global singleton. e.g. if you are running multiple tests or simulations in the same process.