Skip to content Skip to main navigation Skip to footer

Python: python使用redis-py-cluster操作redis cluster集群

redis3.0的集群已经搭建完了,那么开始用python来操作redis cluster集群试试,但是默认的redis模块已经是不能在使用了。  会提示下面的错误…..

Python: python使用redis-py-cluster操作redis cluster集群
Python: python使用redis-py-cluster操作redis cluster集群

In [1]: import redis

In [2]: r = redis.StrictRedis(host=’localhost’, port=7000)

In [3]: r.set(‘a’,’a’)

—————————————————————————

ResponseError                             Traceback (most recent call last)

in ()

—-> 1 r.set(‘a’,’a’)

/usr/local/lib/python2.7/dist-packages/redis/client.pyc in set(self, name, value, ex, px, nx, xx)

1053         if xx:

1054             pieces.append(‘XX’)

-> 1055         return self.execute_command(‘SET’, *pieces)

1056

1057     def __setitem__(self, name, value):

/usr/local/lib/python2.7/dist-packages/redis/client.pyc in execute_command(self, *args, **options)

563         try:

564             connection.send_command(*args)

–> 565             return self.parse_response(connection, command_name, **options)

566         except (ConnectionError, TimeoutError) as e:

567             connection.disconnect()

/usr/local/lib/python2.7/dist-packages/redis/client.pyc in parse_response(self, connection, command_name, **options)

575     def parse_response(self, connection, command_name, **options):

576         “Parses a response from the Redis server”

–> 577         response = connection.read_response()

578         if command_name in self.response_callbacks:

579             return self.response_callbacks[command_name](response, **options)

/usr/local/lib/python2.7/dist-packages/redis/connection.pyc in read_response(self)

572             raise

573         if isinstance(response, ResponseError):

–> 574             raise response

575         return response

576

ResponseError: MOVED 15495 127.0.0.1:7002

妈蛋的, pip install redis-py-cluster    作者貌似pypi的版本有些旧,不能用….  提示StrictRedisCluster 无法找到….  github上的代码和pypi确实不太一样…..    可以把源码git clone下来,然后python setup.py install 安装最近的代码.

Python

In [1]: from rediscluster import StrictRedisCluster
In [2]: startup_nodes = [{"host>": "127.0.0.1>", "port>": "7000>"}]
In [3]: rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
In [4]: rc.set("foo>", "xiaorui.cc>")
Out[4]: True
In [5]: rc.set("foo1>", "blog.xiaorui.cc>")
Out[5]: True
In [6]: rc.set("foo2>", "bar>")
Out[6]: True
In [7]: rc.set("foo3>", "bar>")
Out[7]: True 

这么看来   ASK/MOVE 是没有问题的,不像原版的redis python那样,出现move error的问题…   

貌似redis-py-cluster 不支持事务(watch multi exec) , 有些蛋疼。 

In [10]: pipe = rc.pipeline()

In [11]: pipe.watch(‘xiaorui.cc’)

—————————————————————————

RedisClusterException                     Traceback (most recent call last)

in ()

—-> 1 pipe.watch(‘xiaorui.cc’)

/root/redis-py-cluster/rediscluster/pipeline.pyc in watch(self, *names)

266

267     def watch(self, *names):

–> 268         raise RedisClusterException(“method watch() is not implemented”)

269

270     def unwatch(self):

RedisClusterException: method watch() is not implemented

就这样吧….

原文:http://xiaorui.cc/2015/05/16/python使用redis-py-cluster操作redis-cluster集群/

0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.