forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pub_sub_system.py
31 lines (24 loc) · 924 Bytes
/
pub_sub_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from concurrent.futures import ThreadPoolExecutor
from topic import Topic
class PubSubSystem:
def __init__(self):
self.topics = {}
self.executor_service = ThreadPoolExecutor(max_workers=10)
def create_topic(self, topic_name):
self.topics.setdefault(topic_name, Topic(topic_name))
def subscribe(self, topic_name, subscriber):
topic = self.topics.get(topic_name)
if topic:
topic.add_subscriber(subscriber)
def unsubscribe(self, topic_name, subscriber):
topic = self.topics.get(topic_name)
if topic:
topic.remove_subscriber(subscriber)
def publish(self, topic_name, message):
topic = self.topics.get(topic_name)
if topic:
self.executor_service.submit(topic.publish, message)
def shutdown(self):
self.executor_service.shutdown()
def get_topics(self):
return self.topics