redis_config
1 Redis 连接
1.1 配置文件
butterfly/conf/servicer/redis_xx.py
url_xx = "redis://@localhost:6379/0?socket_timeout=0.2&socket_connect_timeout=0.1&retry_on_timeout=false"1.2 读取配置
from xlib import db
_db = db.redisdb_get(None, "redis_xx", "url_xx")
cache = _db.cache()
key = "ceshi"
value = "ceshi"
timeout = 2
# True
print cache.set(key, value, timeout)
# ceshi
print cache.get(key)
# 1
print cache.delete(key)2 Redis连接配置中的超时配置
socket_connect_timeout
socket_timeout
retry_on_timeout
2.1 socket_timeout
此配置参数是指
Redis发出命令接收响应的时间不能超过此参数设置时间. 如果超过了此时间, 将会抛出异常:redis.exceptions.TimeoutError: Timeout reading from socket, 即读取响应超时.如何来演示
socket_timeout触发的超时问题呢? 可以从我们经常在list类型数据上进行BLPOP操作着手.如上所示: 当
test_key列表为空时,BLPOP命令将会进行阻塞, 阻塞的timeout设置为10秒, 但Redis连接的socket_timeout设置为5秒. 此时会触发异常.原因如下: 当我们在进行
BLPOP操作时, 阻塞的过程其实也是等待响应的过程(waiting for reading from socket). 因为socket_timeout设置为5秒, 所以阻塞5秒后会触发超时异常, 而非等待10秒后得到的None.解决方式: 将类
BLPOP型命令的超时时间设置少于socket_timeout设置的时间即可.实际业务中遇到此问题是在使用
redis_lock过程中, 其实原理和上述一致(实际redis_lock底层也是使用了BLPOP命令), 只是锁的过期时间需要小于socket_timeout设置的时间.示例代码如下:
2.2 socket_connect_timeout
此配置参数指
Redis建立连接超时时间. 当设置此参数时, 如果在此时间内没有建立连接, 将会抛出异常redis.exceptions.TimeoutError: Timeout connecting to server.可以用以下代码做测试, 比如, 将
socket_connect_timeout设置尽可能小, 则很容易模拟实际的连接超时问题.对
redis-py源码进行分析, 实际redis建立TCP连接使用socket包进行的连接. 源码如下:
2.3 retry_on_timeout
此参数为布尔型. 默认为
False.当设置
False时, 一个命令超时后, 将会直接抛出timeout异常.当设置为
True时, 命令超时后,将会重试一次, 重试成功则正常返回; 失败则抛出timeout异常.
引用
Last updated