Skip to content

Instantly share code, notes, and snippets.

@tonyroberts
Last active July 15, 2020 13:30
Show Gist options
  • Save tonyroberts/4603431fe74c8ed860683b44ea896047 to your computer and use it in GitHub Desktop.
Save tonyroberts/4603431fe74c8ed860683b44ea896047 to your computer and use it in GitHub Desktop.
# Use a single global message broker for all topics
_global_message_broker = MessageBroker()
def publish(topic, value):
"""Publishes a value on a topic for a subscriber to receive"""
_global_message_broker.publish(topic, value)
def subscribe(topic, callback):
"""Subscribes to a topic.
The subscriber callback is called with the topic value whenever
the a value is published to that topic, and with the initial
value if there is one.
"""
_global_message_broker.subscribe(topic, callback)
def unsubscribe(topic, callback):
"""Unsubscribe from a topic.
The topic and callback must have previously be passed to 'subscribe'.
"""
_global_message_broker.unsubscribe(topic, callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment