Skip to content

Instantly share code, notes, and snippets.

@jnrbsn
Created June 18, 2014 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnrbsn/8fb5d9c1068b02674560 to your computer and use it in GitHub Desktop.
Save jnrbsn/8fb5d9c1068b02674560 to your computer and use it in GitHub Desktop.
Re-establish a redis pubsub connection after losing it
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