From 4c237d03ddf79d1f5e7d61e9c105976bcdd14f15 Mon Sep 17 00:00:00 2001 From: Jonas Hagstedt Date: Sat, 30 Aug 2014 21:01:40 +0200 Subject: [PATCH 1/2] Ensure that there are no subscribers if there is no connection to Redis --- tornadoredis/pubsub.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tornadoredis/pubsub.py b/tornadoredis/pubsub.py index 2ff89bb..4cdac36 100644 --- a/tornadoredis/pubsub.py +++ b/tornadoredis/pubsub.py @@ -38,6 +38,14 @@ def subscribe(self, channel_name, subscriber, callback=None): subscriber - a method or object to be used by on_message handler callback - a callback function """ + + # If for some reason the redis client is not connected (the server might have gone down) + # then clear the subscribers. This ensure that there are no subscribers since there possibly can't be an + # active Redis subscription + if not self.redis.connection.connected(): + self.subscribers = defaultdict(Counter) + self.subscriber_count = Counter() + if isinstance(channel_name, list) or isinstance(channel_name, tuple): if len(channel_name) > 1: _cb = lambda *args, **kwargs: self.subscribe(channel_name[1:], @@ -49,6 +57,7 @@ def subscribe(self, channel_name, subscriber, callback=None): else: self.subscribers[channel_name][subscriber] += 1 self.subscriber_count[channel_name] += 1 + print(self.subscriber_count[channel_name]) if self.subscriber_count[channel_name] == 1: if not self.redis.subscribed: if callback: From 0f6c5b77b873745761d9f6c59f110c37c46cdd63 Mon Sep 17 00:00:00 2001 From: Jonas Hagstedt Date: Sat, 30 Aug 2014 21:02:52 +0200 Subject: [PATCH 2/2] Removed print statement showing number of subscribers of a channel --- tornadoredis/pubsub.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tornadoredis/pubsub.py b/tornadoredis/pubsub.py index 4cdac36..66cd52f 100644 --- a/tornadoredis/pubsub.py +++ b/tornadoredis/pubsub.py @@ -57,7 +57,6 @@ def subscribe(self, channel_name, subscriber, callback=None): else: self.subscribers[channel_name][subscriber] += 1 self.subscriber_count[channel_name] += 1 - print(self.subscriber_count[channel_name]) if self.subscriber_count[channel_name] == 1: if not self.redis.subscribed: if callback: