首页 技术 正文
技术 2022年11月21日
0 收藏 949 点赞 2,440 浏览 3656 个字

问题描述

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]

参考资料

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848