publish-subscribe pattern def publish(selfmessage)self dispatcher send(messageclass dispatcher(object)def __init__(self)self subscribers set(def subscribe(selfsubscriber)self subscribers add(subscriberdef unsubscribe(selfsubscriber)self subscribers discard(subscriberdef unsubscribe_all(self)self subscribers set(def send(selfmessage)for subscriber in self subscriberssubscriber process(messagenot all subscribers are interested in all the messages from all publishers since the point of this whole exercise was to decouple subscribers from publisherswe do not want to couple them together in any way the solution we are looking for should allow the dispatcher to send categories of messages to specific subscribers we are going to add topic to the messages publishers will now add topic to the messages they publishand subscribers can then subscribe to specific topics class message(object)def __init__(self)self payload none self topic "allclass subscriber(object)def __init__(selfdispatchertopic)dispatcher subscribe(selftopicdef process(selfmessage)print("message{}format(message payload)