-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpubsub.py
67 lines (49 loc) · 1.68 KB
/
pubsub.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
""" pubsub.py -- very cheap publish/subscribe
Copyright (C) 2012 Phil Mayes
See COPYING.txt for licensing information
Contact: phil@philmayes.com
Topics are string keys in a dictionary. The value for a topic is in turn
a dictionary whose keys are the registered callback functions. Their values
have no meaning.
"""
topics = {}
def publish(topic, *args, **kwargs):
if topic in topics:
for consumer in topics[topic]:
consumer(*args, **kwargs)
def subscribe(topic, consumer):
"""Register callable <consumer> for string <topic>."""
# add topic to the dictionary if it does not yet exist
if not topic in topics:
topics[topic] = {}
# get the dictionary of consumers for this topic
consumers = topics[topic]
# register this consumer
consumers[consumer] = 1
def unsubscribe(topic, consumer):
"""Remove topic + consumer from the dictionary if it exists."""
if topic in topics:
# remove consumer for this topic; OK if does not exist
topics[topic].pop(consumer, 0)
def test():
def callback1(data):
print("callback1", data)
def callback2(data, d2):
print("callback2", data, d2)
def callback3(*args, **kwargs):
print("callback3", args, kwargs)
# subscribe the above callbacks to various topics
subscribe("alpha", callback1)
subscribe("beta", callback2)
subscribe("beta", callback3)
subscribe("unpublished", callback3)
# publish various topics
publish("alpha", 111)
publish("beta", [12, 23, 34], "string!")
publish("gamma", "three")
print(topics)
unsubscribe("beta", callback3)
print(topics)
if __name__ == "__main__":
test()