Skip to content

Instantly share code, notes, and snippets.

@tonyroberts
Created July 15, 2020 13:08
Show Gist options
  • Save tonyroberts/7caba8aa24e1c57abe59e3f88ad113c1 to your computer and use it in GitHub Desktop.
Save tonyroberts/7caba8aa24e1c57abe59e3f88ad113c1 to your computer and use it in GitHub Desktop.
class MessageBroker:
def __init__(self):
self.__subscribers = {}
def publish(self, topic, message):
for callback in self.__subscribers.get(topic, []):
callback(message)
def subscribe(self, topic, callback):
subscribers = self.__subscribers.setdefault(topic, [])
subscribers.append(callback)
def unsubscribe(self, topic, callback):
subscribers = self.__subscribers[topic]
subscribers.remove(callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment