Python – How to check if Redis server is available

If you want to test redis connection once at startup, use the ping() command.

from redis import Redis

redis_host="127.0.0.1"
r = Redis(redis_host, socket_connect_timeout=1) # short timeout for the test

r.ping() 

print('connected to redis "{}"'.format(redis_host)) 

The command ping() checks the connection and if invalid will raise an exception.

  • Note – the connection may still fail after you perform the test so this is not going to cover up later timeout exceptions.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)