问题描述

Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer" "ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host" 

Traceback (most recent call last):
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\redis\connection.py", line 748, in read_response
response = self._parser.read_response()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\redis\connection.py", line 318, in read_response
raw = self._buffer.readline()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\redis\connection.py", line 250, in readline
self._read_from_socket()
File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\redis\connection.py", line 192, in _read_from_socket
data =ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host... ...

连接出错的Python代码为:

import redis

redis_params = {
'host': 'redis-xxxxxx.redis.cache.chinacloudapi.cn',
'port': 6380,
'db': 1,
'password': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=',
'timeout': 5000,
'ssl': True
}

print('-----Try # 2------: Redis 使用连接池 ')

print('connected to redis by connection pool ...')
pool = redis.ConnectionPool( host=redis_params['host'], port=redis_params['port'], db=redis_params['db'], password=redis_params['password'], max_connections=20)
conn_pool = redis.Redis(connection_pool=pool, socket_timeout=redis_params['timeout'], ssl=redis_params['ssl'], health_check_interval=30)


conn_pool.set('test03', 'Value 结果显示在此')
data = conn_pool.get('test03').decode()
print(data)

print('Connection Pool use Successful')

 

而在不使用 ConnectionPool 的情况下,是能够正常连接的。

print('-----Try # 1------: Redis 没有使用连接池的情况下: ')
conn = redis.Redis(host=redis_params['host'], port=redis_params['port'], db=redis_params['db'], password=redis_params['password'], ssl=redis_params['ssl'], socket_timeout=redis_params['timeout'])
print('connected to redis')
conn.set('test', 'the normal value to set ')
print('set done .... ')

 

问题解答

根据redis.py的源码查看,使用redis.Redis 当ssl为True时,构造函数中为 connection_class参数 初始化值为 SSLConnection。 而ConnectionPool的构造函数中默认使用的是 connection_class=Connection。

redis.Redis

【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现_Azure Redis

redis.ConnectionPool

【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现_python_02

根据以上分析,要解决Python Redis使用Connection Pool连接,只需要在创建Connection Pool对象时候,为Connection_class对象赋值为SSLConnection。

修改后的代码为:

import redis

from redis.connection import SSLConnection= {
'host': 'redis-xxxxxx.redis.cache.chinacloudapi.cn',
'port': 6380,
'db': 1,
'password': 'xxxxxxxxxxxxxxxxxxx=',
'timeout': 5000,
'ssl': True
}

print('-----Try # 1------: Redis 没有使用连接池的情况下: ')
conn = redis.Redis(host=redis_params['host'], port=redis_params['port'], db=redis_params['db'], password=redis_params['password'], ssl=redis_params['ssl'], socket_timeout=redis_params['timeout'])
print('connected to redis')
conn.set('test', 'the normal value to set ')
print('set done .... ')

data = conn.get('test').decode()
print(str(data))

print('-----Try # 2------: Redis 使用连接池 ')

print('connected to redis by connection pool ...')
pool = redis.ConnectionPool(connection_class= SSLConnection, host=redis_params['host'], port=redis_params['port'], db=redis_params['db'], password=redis_params['password'], max_connections=20)
conn_pool = redis.Redis(connection_pool=pool, socket_timeout=redis_params['timeout'], ssl=redis_params['ssl'], health_check_interval=30)


conn_pool.set('test03', 'Value 结果显示在此')
data = conn_pool.get('test03').decode()
print(data)

print('Connection Pool use Successful')

for i in range(1,10):
conn_pool_1 = redis.Redis(connection_pool=pool, socket_timeout=redis_params['timeout'], ssl=redis_params['ssl'], health_check_interval=30)
conn_pool_1.set('test_test__'+str(i), 'use conn pool to set value.......')
print(conn_pool_1.client)


clist = conn_pool.client_list()
for c in clist:
print(c)

PS: 添加黄色高亮部分即可。

整体演示动画如下:

【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现_Azure Redis_03

[END]

 

参考资料

 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!