Created
June 18, 2014 17:03
-
-
Save jnrbsn/8fb5d9c1068b02674560 to your computer and use it in GitHub Desktop.
Re-establish a redis pubsub connection after losing it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from redis import StrictRedis, ConnectionError | |
channels = ['test'] | |
redis = StrictRedis() | |
pubsub = redis.pubsub() | |
pubsub.subscribe(channels) | |
while True: | |
try: | |
for item in pubsub.listen(): | |
print(item) | |
except ConnectionError: | |
while True: | |
print('lost connection; trying to reconnect...') | |
try: | |
redis.ping() | |
except ConnectionError: | |
time.sleep(10) | |
else: | |
pubsub.subscribe(channels) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment