python redis连接池sub/pub断开连接问题
单机redis服务器没有出现断开连接问题
云服务集群版本出现了
尝试解决
连接池设置探测活性
# -*- coding: utf-8 -*-
'''
该模块是对依赖环境的封装,包括redis等等
'''
from _imports import *import redis
import platform# import module
import settings
# redis
socket_keepalive_options = None
if platform.system().lower() == "linux":
socket_keepalive_options = {socket.TCP_KEEPIDLE: 3,
socket.TCP_KEEPCNT: 2,
socket.TCP_KEEPINTVL:1}
pass
redis_config = settings.redis_config
redis_connection_pool = redis.ConnectionPool(max_connections=64,
host=redis_config['host'],
port=redis_config['port'],
db=redis_config['db'],
password=redis_config['password'],
socket_timeout=1,
socket_connect_timeout=1,
socket_keepalive=True, # enable keepalive
socket_keepalive_options=socket_keepalive_options) #redis_client = redis.StrictRedis(connection_pool=redis_connection_pool)
redis_client.ping()
其中参数解释如下
int fd = tcpSocket->socketDescriptor();
int keepAlive = 1; //开启keepalive属性,缺省值:0(关闭)
int keepIdle = 5; //如果在5秒内没有任何数据交互,则进行探测,缺省值:7200(s)
int keepInterval = 2; //探测时发探测包的时间间隔为2秒,缺省值:75(s)
int keepCount = 2; //探测重试的次数,全部超时则认定连接失效,缺省值:9(次)
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive, sizeof(keepAlive));
setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, (void *)&keepIdle, sizeof(keepIdle));
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval, sizeof(keepInterval));
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount, sizeof(keepCount));