在一次分享中提到了Redis的长短链接的问题,引发了对redis-py的连接池机制的讨论。
通过源码发现,每创建一个Redis实例都会构造出一个ConnectionPool,每一次访问redis都会从这个连接池得到一个连接,访问完成之后,会把该连接放回连接池,下面是发送命令访问redis的execute_command方法实现:
352 #### COMMAND EXECUTION AND PROTOCOL PARSING ####
353 def execute_command(self, *args, **options):
354 "Execute a command and return a parsed response"
355 pool = self.connection_pool
356 command_name = args[0]
357 connection = pool.get_connection(command_name, **options)
358 try:
359 connection.send_command(*args)
360 return self.parse_response(connection, command_name, **options)
361 except ConnectionError:
362 connection.disconnect()
363 connection.send_command(*args)
364 return self.parse_response(connection, command_name, **options)
365 finally:
366 pool.release(connection)
当然,也可以构造一个ConnectionPool,在创建Redis实例时,可以将该ConnectionPool传入,那么后续的操作会从给定的ConnectionPool获得连接。
redis-py的作者在文档中也有详细说明:
Connection Pools
Behind the scenes, redis-py uses a connection pool to manage connections to a Redis server. By default, each Redis instance you create will in turn create its own connection pool. You can override this behavior and use an existing connection pool by passing an already created connection pool instance to the connection_pool argument of the Redis class. You may choose to do this in order to implement client side sharding or have finer grain control of how connections are managed.
>>> pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
>>> r = redis.Redis(connection_pool=pool)
关于redis-server的最大客户端数量问题
redis的文档这样说:
# Set the max number of connected clients at the same time. By default there
# is no limit, and it's up to the number of file descriptors the Redis process
# is able to open. The special value '0' means no limits.
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# maxclients 128
1) redis默认没有设置最大的连接客户端数量,这个数量取决于redis进程能够打开的文件句柄数量。
2) 可以手工配置最大的连接池数量。
分享到:
相关推荐
redis-py是Redis key-value 数据库的 Python 接口,安装如下,后面我们会讲hiredis这个库 代码如下: $ sudo pip install redis $ sudo pip install hiredis 2.入门 代码如下: >>> import redis >>> pool = redis....
这些类型的应用程序通常需要与多个Redis服务器的容错连接池,从而可以轻松开发和维护分布式系统。 支持大多数以及其他功能,例如静默重新连接,连接池和自动分片。 该驱动程序作为Web框架的一部分分发。 变更日志...
使用本机redis-py url表示法连接字符串可插拔客户端可插拔解析器可插拔串行器默认客户端中的主要/辅助支持全面的测试套件在多个项目的生产中用作缓存和会话存储支持无限超时原始访问Redis客户端/连接池的设施高度可...
考虑到我们已经习惯于使用redis-py连接redis,我们可以在redis-py客户端的基础上更改连接池。 安装 要从源代码安装pycodis: $ sudo python setup.py install 如何使用 >>> import redis >>> from codis import ...
连接池 加密ssl SET选项EX seconds-设置指定的到期时间(以秒为单位)。 SET选项PX毫秒-设置指定的到期时间(以毫秒为单位)。 SET选项EXAT timestamp-seconds-设置密钥过期的指定Unix时间(以秒为
特征特征支持的hiredis解析器 :check_mark_button: 纯Python解析器 :check_mark_button: 低级和高级API :check_mark_button: 流水线支持 :check_mark_button: 多/执行支持 :check_mark_button: 连接池 :check_mark_...
python入门到高级全栈工程师培训视频学习资料;本资料仅用于学习,请查看后24小时之内删除。 【课程内容】 第1章 01 计算机发展史 02 计算机系统 03 小结 04 数据的概念 05 进制转换 06 原码补码反码 07 物理层和...
技术选型 1、后端 核心框架:Spring Boot 安全框架:Apache Shiro 模板引擎:Thymeleaf 持久层框架:MyBatis 数据库连接池:Alibaba Druid 缓存框架:Ehcache 、Redis 日志管理:SLF4J 工具类:Apache Commons、...
最新Python3.5零基础+高级+完整项目(28周全)培训视频学习资料;本资料仅用于学习。 【课程内容】 第1周 开课介绍 python发展介绍 第一个python程序 变量 字符编码与二进制 字符编码的区别与介绍 用户交互程序 if ...
系统监控: 操作日志、登录日志、系统日志、在线用户、服务监控、连接池监控、缓存监控等 产品管理: 产品、产品物模型、产品分类、产品固件、设备授权码等 设备管理: 控制、分组、定时、日志、统计、定位、分享、配置...