2808 explained code solutions for 75 technologies


python-redisAuto reconnect to Redis server


We're demonstrating an infinite loop which can meet situation of Redis server being unavailable:

import redis
import time

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)

while True:
    try:
        r = redis.Redis(connection_pool=pool)
        print(r.ping())
    except Exception as e:
        print(e)

    time.sleep(1)ctrl + c
import redis

import Redis module

redis.ConnectionPool

creates Redis connection pool to be used later

redis.Redis

connect to Redis server using connection pool (will automatically reconnect when server is available again)

r.ping()

pings Redis server (will return True on success)

except Exception as e

intercept and print error message so the script will not break

time.sleep(1)

wait for 1 second till next iteration


Usage example

import redis
import time

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)

while True:
    try:
        r = redis.Redis(connection_pool=pool)
        print(r.ping())
    except Exception as e:
        print(e)

    time.sleep(1)
output
True
True
True
True
True
True
Error 111 connecting to localhost:6379. Connection refused.
True
True
...