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()
  • 相关阅读:
    台式机+笔记本的扩展模式+远程登录设置
    Hadoop 集群搭建以及脚本撰写
    Python 入门学习(三)
    1056 Mice and Rice
    1057 Stack
    1058 A+B in Hogwarts
    1059 Prime Factors
    使用熔断器仪表盘监控
    使用熔断器防止服务雪崩
    创建服务消费者(Feign)
  • 原文地址:https://www.cnblogs.com/sthu/p/10207858.html
Copyright © 2011-2022 走看看