skip to Main Content

I’m trying the connect to a redis cluster with the below code.

import redis

ssl_ca_certs='<path_to_ca_certfile>'

r = redis.RedisCluster(
  host='<RedisHOST>',
  port=6379,
  ssl=True,
  password='<password>',
  ssl_ca_certs=ssl_ca_certs
  )

The code was working fine for some time. but recently I’m getting error

Traceback (most recent call last):
File "/home/rnatarajan/network-signals/py-demo/get-redis-cli.py", line 7, in

r = redis.RedisCluster(

AttributeError: module ‘redis’ has no attribute ‘RedisCluster’

I tried uninstalling and reinstalling the redis package.

I uninstalled redis-py-cluster package.

Note: I’m using ubuntu 22.04

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:    22.04
Codename:   jammy

I’m using python 3.10

$ python3 --version
Python 3.10.12

Any idea how to fix this error?

2

Answers


  1. I think it should be used like this:

        redis.cluster.RedisCluster(...)
    
    Login or Signup to reply.
  2. redis-py (pip install redis) now supports clustering so you shouldn’t need an additional package. Try something like this:

    from redis.cluster import RedisCluster as Redis
    rc = Redis(host='localhost', port=6379)
    print(rc.get_nodes())
        [[host=127.0.0.1,port=6379,name=127.0.0.1:6379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6379,db=0>>>], [host=127.0.0.1,port=6378,name=127.0.0.1:6378,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6378,db=0>>>], [host=127.0.0.1,port=6377,name=127.0.0.1:6377,server_type=replica,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=6377,db=0>>>]]
    

    Reference: https://redis.readthedocs.io/en/stable/clustering.html#

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search