【Azure Redis 快取】 Python連線Azure Redis, 使用redis.ConnectionPool 出現 "ConnectionResetError: [Errno 104] Connection reset by peer"

路邊兩盞燈發表於2022-05-11

問題描述

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 = self._sock.recv(socket_read_size)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:
... ... 

連線出錯的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 出現 "ConnectionResetError: [Errno 104] Connection reset by peer"

redis.ConnectionPool

【Azure Redis 快取】 Python連線Azure Redis, 使用redis.ConnectionPool 出現 "ConnectionResetError: [Errno 104] Connection reset by peer"

根據以上分析,要解決Python Redis使用Connection Pool連線,只需要在建立Connection Pool物件時候,為Connection_class物件賦值為SSLConnection。

修改後的程式碼為:

import redis

from redis.connection import SSLConnection

redis_params = {
    '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 出現 "ConnectionResetError: [Errno 104] Connection reset by peer"

[END]

 

參考資料

 

相關文章