【AzureRedis缓存】Python连接AzureRedis,使用redis.ConnectionPool出现"ConnectionResetError:[Errno104]Connectionresetbypeer"

博客 动态
0 196
羽尘
羽尘 2022-05-11 20:59:06
悬赏:0 积分 收藏

【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"

问题描述

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 hostDuring handling of the above exception, another exception occurred:... ... 

连接出错的Python代码为:

import redisredis_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

redis.ConnectionPool

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

修改后的代码为:

import redisfrom redis.connection import SSLConnectionredis_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: 添加黄色高亮部分即可。

整体演示动画如下:

[END]

 

参考资料

 

posted @ 2022-05-11 20:44 路边两盏灯 阅读(0) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员