Skip to content

Commit

Permalink
detect closed connection (on the server side)
Browse files Browse the repository at this point in the history
  • Loading branch information
thefab committed Aug 25, 2015
1 parent 8a9ee8d commit 5c407d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions examples/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
@tornado.gen.coroutine
def pubsub_coroutine():
# Let's get a connected client
client = tornadis.PubSubClient()
# we don't use autoconnect=True because issue #22
client = tornadis.PubSubClient(autoconnect=False)
yield client.connect()

# Let's "psubscribe" to a pattern
yield client.pubsub_psubscribe("foo*")
Expand All @@ -20,7 +22,11 @@ def pubsub_coroutine():
print(msg)
# >>> ['pmessage', 'foo*', 'foo', 'bar']
# (for a "publish foo bar" command from another connection)
if len(msg) >= 4 and msg[3] == "STOP":

if isinstance(msg, tornadis.TornadisException):
# closed connection by the server
break
elif len(msg) >= 4 and msg[3] == "STOP":
# it's a STOP message, let's unsubscribe and quit the loop
yield client.pubsub_punsubscribe("foo*")
yield client.pubsub_unsubscribe("bar")
Expand Down
4 changes: 4 additions & 0 deletions tornadis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def _close_callback(self):
callback(ConnectionError("closed connection"))
except IndexError:
break
if self.subscribed:
# pubsub clients
self._reply_list.append(ConnectionError("closed connection"))
self._condition.notify_all()

def _read_callback(self, data=None):
"""Callback called when some data are read on the socket.
Expand Down

0 comments on commit 5c407d5

Please sign in to comment.