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

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

关于python redis-py-cluster操作redis cluster集群原文地址 , http://xiaorui.cc/2015/05/16/python%E4%BD%BF%E7%94%A8redis-py-cluster%E6%93%8D%E4%BD%9Credis-cluster%E9%9B%86%E7%BE%A4/

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)
<ipython-input-3-87a61267ce52> in <module>()
—-> 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 安装最近的代码.


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)
<ipython-input-11-a39f0a5f9fa7> in <module>()
—-> 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



对于python redis cluster模块就先这样吧,后续如遇到一些奇怪的问题和用法会更新此文章的….






大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

邮箱地址不会被公开。 必填项已用*标注