zoukankan      html  css  js  c++  java
  • python连接redis,redis集群

    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()
  • 相关阅读:
    约瑟夫
    用过的ps操作
    guns框架试用笔记
    让使用WebForm的.aspx文件写的WebApi能够跨域访问
    DevExpress的GridView的行变和列变
    SSMS18.0缺少调试功能
    EF_CodeFirst框架版本问题
    微信小程序框架了解2---js的写法
    微信小程序框架了解1---总体了解
    Chrome浏览器写代码片段的地方
  • 原文地址:https://www.cnblogs.com/lelexiong/p/9940094.html
Copyright © 2011-2022 走看看