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

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 【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]

 

参考资料

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
8天前
|
NoSQL Linux Redis
linux安装单机版redis详细步骤,及python连接redis案例
这篇文章提供了在Linux系统中安装单机版Redis的详细步骤,并展示了如何配置Redis为systemctl启动,以及使用Python连接Redis进行数据操作的案例。
19 2
|
8天前
|
Unix Linux 网络安全
python中连接linux好用的模块paramiko(附带案例)
该文章详细介绍了如何使用Python的Paramiko模块来连接Linux服务器,包括安装配置及通过密码或密钥进行身份验证的示例。
13 1
|
2月前
|
NoSQL 安全 网络安全
【Azure Redis】PHPRedis遇见SSL Connection Timeout问题
【Azure Redis】PHPRedis遇见SSL Connection Timeout问题
【Azure Redis】PHPRedis遇见SSL Connection Timeout问题
|
8天前
|
Linux Python
Linux之centos安装clinkhouse以及python如何连接
Linux之centos安装clinkhouse以及python如何连接
|
2月前
|
缓存 NoSQL Linux
【Azure Redis 缓存】应用中出现连接Redis服务错误(production.ERROR: Connection refused)的排查步骤
【Azure Redis 缓存】应用中出现连接Redis服务错误(production.ERROR: Connection refused)的排查步骤
|
19天前
|
canal 缓存 NoSQL
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
根据对一致性的要求程度,提出多种解决方案:同步删除、同步删除+可靠消息、延时双删、异步监听+可靠消息、多重保障方案
Redis缓存与数据库如何保证一致性?同步删除+延时双删+异步监听+多重保障方案
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
【Azure Redis 缓存】Redission客户端连接Azure:客户端出现 Unable to send PING command over channel
|
2月前
|
缓存 NoSQL Java
Redis深度解析:解锁高性能缓存的终极武器,让你的应用飞起来
【8月更文挑战第29天】本文从基本概念入手,通过实战示例、原理解析和高级使用技巧,全面讲解Redis这一高性能键值对数据库。Redis基于内存存储,支持多种数据结构,如字符串、列表和哈希表等,常用于数据库、缓存及消息队列。文中详细介绍了如何在Spring Boot项目中集成Redis,并展示了其工作原理、缓存实现方法及高级特性,如事务、发布/订阅、Lua脚本和集群等,帮助读者从入门到精通Redis,大幅提升应用性能与可扩展性。
60 0
|
2月前
|
缓存 NoSQL Redis
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
【Azure Redis 缓存】使用StackExchange.Redis,偶发ERROR - Timeout performing HSET (15000ms)
|
20天前
|
存储 NoSQL Redis
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
Redis持久化、RDB和AOF方案、Redis主从集群、哨兵、分片集群、散列插槽、自动手动故障转移
SpringCloud基础7——Redis分布式缓存,RDB,AOF持久化+主从+哨兵+分片集群
下一篇
无影云桌面