redis - python
作者:互联网
pip install redis-py-cluster==1.3.6
# Redis实例创建及管道
# Note: 与python3一起使用时,decode_responses必须设置为True
# max_connections最大连接数,如果设20,那么在建立20个连接时会报错。
import redis, rediscluster
# 主从模式
pool = redis.ConnectionPool(host=xxx, port=xxx, socket_timeout=xx, db=x, password=x, decode_responses=False) # 多个redis实例共享一个连接池。
r = redis.StrictRedis(connection_pool=pool, decode_responses=False) # redis.Redis是StrictRedis的子类,用于向后兼容旧版本的redis-py
pipe = r.pipeline(transaction=True) # 默认执行每次请求都会创建(pool申请连接)和断开(归还pool)操作。用pipline实现一次请求指定多个命令.
pipe.set('key', 'value')
pipe.execute() # 批量,数量限制?
# redis集群模式
rc = rediscluster_xx.StrictRedisCluster(cluster=XX, db=XX, fail_count_to_reload=xx, fail_time_to_reload=xx) # ???
rc = rediscluster.StrictRedisCluster(startup_nodes=[{"host": "xx", "port": xx}], skip_full_coverage_check=True, socket_timeout=timeout, socket_connect_timeout=timeout)
rc = rediscluster.StrictRedisCluster(host='xx', port=xx, socket_timeout=5, skip_full_coverage_check=True)
{
# 连接池
from rediscluster.connection import *
pool = ClusterConnectionPool(startup_nodes=[{"host": "xx", "port": xx}], password="*", socket_timeout=5, db=0, decode_responses=False, skip_full_coverage_check=True) # connection_class=SSLClusterConnection
r =rediscluster.StrictRedisCluster(connection_pool=pool)
}
- redis.exceptions.ResponseError: MOVED 7074 ip:port 启动redis-cli时没有设置集群模式所导致
- rediscluster.exceptions.RedisClusterException: ERROR sending 'config get cluster-require-full-coverage' command to redis server: 设置skip_full_coverage_check=True
- error: transaction is deprecated in cluster mode. cause: pipe = rc.pipeline() 管道在集群模式与正常模式下不同。创建此对象的副本,以便模拟管道正常工作。每个命令将在使用时直接调用,并且在调用execute()时仅返回结果堆栈。
标签:python,redis,rediscluster,xx,timeout,True,pool 来源: https://www.cnblogs.com/bsszds930/p/13033372.html