diff options
Diffstat (limited to 'pubsub.py')
-rw-r--r-- | pubsub.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/pubsub.py b/pubsub.py new file mode 100644 index 0000000..6b860e0 --- /dev/null +++ b/pubsub.py @@ -0,0 +1,17 @@ +class Publisher(object): + def __init__(self): + self.subscribers = [] + + def subscribe(self, who): + if who not in self.subscribers: + self.subscribers.append(who) + + def unsubscribe(self, who): + if who in self.subscribers: + self.subscribers.remove(who) + + def publish(self, message, *args): + for subscriber in self.subscribers: + method = "msg_" + message + if hasattr(subscriber, method): + getattr(subscriber, method)(*args) |