【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 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
redis.ConnectionPool

根据以上分析,要解决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: 添加黄色高亮部分即可。
整体演示动画如下:

[END]
参考资料
无
【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"的更多相关文章
- Python 频繁请求问题: [Errno 104] Connection reset by peer
Table of Contents 1. 记遇到的一个问题:[Errno 104] Connection reset by peer 记遇到的一个问题:[Errno 104] Connection r ...
- python requests [Errno 104] Connection reset by peer
有个需求,数据库有个表有将近 几千条 url 记录,每条记录都是一个图片,我需要请求他们拿到每个图片存到本地.一开始我是这么写的(伪代码): import requests for url in ur ...
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager Redis缓存服务器是一款key/value数据库,读11 ...
- python使用pika链接rabbitmq Connection reset by peer 解决办法
记录一下, 最近在用机器学习打算做一个Rest API, 数据存入mongo,任务采用消息队列,rabbitmq 由于引擎采用python编写,所以WEB也直接打算用python编写了,比较省事. W ...
- spring+ibatis问题1—— 程序报错:java.sql.SQLException: Io 异常: Connection reset by peer, socket write error; ”或“java.sql.SQLException 关闭的连接”异常
转自:http://blog.sina.com.cn/s/blog_1549fb0710102whz2.html spring+ibatis程序测试时报错:java.sql.SQLException: ...
- 【airtest, python】报错:requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')),解决方法如下
环境及设备 mac, xcode , iphonex 问题 最近出现一个让人费解的问题,airtest 没跑多长时间,服务就断掉,而且总是报“requests.exceptions.Connectio ...
- (原)Ubuntu连接远程服务器时connection reset by peer
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11086935.html 最近使用ubuntu通过ssh连接服务器时,由于密码错误,多次连接失败后,在 ...
- 【Azure Redis 缓存】由Azure Redis是否可以自定义密码而引申出Azure PaaS的Redis服务是否可以和自建的Redis进行主从配置呢?
问题描述 在自建的Redis服务中,可以通过 config set requirepass <Password> 设置Redis的登录密码,然后使用auth 命令输入密码.操作命令如下: ...
- 左手Mongodb右手Redis 通过python连接mongodb
首先需要安装第三方包pymongo pip install pymongodb """ 通过python连接mongodb数据库 首先需要初始化数据库连接 "& ...
随机推荐
- 说出 5 条 IO 的最佳实践?
IO 对 Java 应用的性能非常重要.理想情况下,你不应该在你应用的关键路径上 避免 IO 操作.下面是一些你应该遵循的 Java IO 最佳实践: a)使用有缓冲区的 IO 类,而不要单独读取字节 ...
- 学习RabbitMQ(三)
1 用户注册后(会立即提示注册成功),过一会发送短信和邮件通知 发布/订阅模型 以上模式一般是用户注册成功后,写入一条数据到mysql,在发送一条消息到MQ! 如果不用消息中间件(或者简单的做成异步发 ...
- SpringCloud个人笔记-01-Eureka初体验
eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳,在默认情况下erureka server也是一个eureka client ,必须要指定一个 serve &l ...
- 【STM32】MDK中寄存器地址名称映射分析
对于MCU,一切底层配置,最终都是在配置寄存器 51单片机访问地址 51单片机经常会引用一个reg51.h的头文件.下面看看它是怎么把名字和寄存器联系在一起的: 1 sfr p0=0x80; 2 p0 ...
- 多页面共用sessionStorage的实现
sessionStorage的局限: sessionStorage是页面级别的,仅在一个标签页生效,如果同一个浏览器同时打开多个标签页,且都访问同一个域名,sessionStorage是不会在这多 ...
- 假期任务一:安装好JAVA开发环境并且在Eclipse上面成功运行HelloWorld程序
(本周主要做了java环境的安装,安装完jdk后又安装了eclipse,平均每天两小时Java吧,这周敲代码的时间比较少,大多是在b站看java入门视频和菜鸟教程的基础语法,也就打开eclipse验证 ...
- msmpeng.exe阻止移动硬盘弹出
MsMpEng.exe 占用 该进程是微软反恶意软件服务的一个可执行文件,用户无法手动停止该进程. 首先运行 eventvwr.msc打开事件查看器,找到警告信息,查看是什么进程在阻止硬盘弹出. ...
- 联机算法和脱机算法[Alg_001]
一.联机算法 1.定义 也叫在线算法,在算法执行过程中的任意时刻,只对要操作的数据进行一次扫描,扫描完成后便此后不再对已经操作过的数据进行保存和记忆. 这种算法有种特点:如果数据是储存在磁盘或者磁 ...
- 基于LAMP离线部署zabbix3.2.11
zabbix是个什么东西这里不再赘述,先安装lamp再安装zabbix. 1. 安装依赖插件(把下面PHP那些依赖库全部都装了) #yum install -y gcc gcc-c++ opens ...
- 前端CSS浮动、定位、溢出、z-index、透明度
一.浮动float 在 CSS 中,任何元素都可以浮动. 浮动元素会生成一个块级框,而不论它本身是何种元素. 关于浮动的两个特点: 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的 ...
