码迷,mamicode.com
首页 > 编程语言 > 详细

python连接redis,redis集群

时间:2018-11-10 19:10:22      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:bubuko   set   from   use   结果   bsp   技术   for   图片   

python连接redis:

import redis

r = redis.Redis(host=‘192.168.50.181‘,port=6002)
r.set(‘user_phone_14900000001‘,‘888888‘)
r.get(‘user_phone_14900000001‘)

 上面代码如果只执行一条数据是正常的,如果要用for循环批量更改数据连接redis集群,会报错redis.exceptions.ResponseError()技术分享图片

 

 

解决方案如下:

python连接redis集群:

from rediscluster import StrictRedisCluster

list = [
    ‘14900000001‘,
    ‘14900000002‘,
    ‘14900000003‘,
    ‘14900000004‘,
    ‘14900000005‘,
    ‘14900000006‘,
    ‘14900000007‘,
    ‘14900000008‘,
    ‘14900000009‘,
    ‘14900000010‘
]

def redis_cluster(list):
    redis_nodes = [
        {‘host‘:‘192.168.50.39‘,‘port‘:6002},
        {‘host‘:‘192.168.50.181‘,‘port‘: 6002},
    ]

    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except:
        print(‘error‘)

    for i in range(0,len(list)):
        redisconn.set(‘user_phone_‘+list[i],‘888888‘)
        print(redisconn.get(‘user_phone_‘+list[i]))

redis_cluster(list)

打印结果如下:

b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘
b‘888888‘

 

 

def redis_cluster():
    redis_nodes = [
        {‘host‘:‘192.168.50.39‘,‘port‘:6002},
        {‘host‘:‘192.168.50.181‘,‘port‘: 6002},
    ]

    try:
        redisconn = StrictRedisCluster(startup_nodes=redis_nodes)
    except:
        print(‘error‘)

    for i in range(14900000011,14900000021):
        redisconn.set(‘user_phone_‘+str(i),‘888888‘)
        print(redisconn.get(‘user_phone_‘+str(i)))

redis_cluster()

 

python连接redis,redis集群

标签:bubuko   set   from   use   结果   bsp   技术   for   图片   

原文地址:https://www.cnblogs.com/lelexiong/p/9940094.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!