侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

关于Redis的Python客户端的连接池问题

Python  /  管理员 发布于 3年前   305

 

在一次分享中提到了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.


  • 上一条:
    redis 学习
    下一条:
    ajax+jsp草稿自动保存的实现代码
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • python爬虫BeautifulSoup快速抓取网站图片(0个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • VSCode Python开发环境配置的详细步骤(0个评论)
    • 使用 Visual Studio Code(VSCode)搭建简单的Python+Django开发环境的方法步骤(0个评论)
    • 解决vscode python print 输出窗口中文乱码的问题(0个评论)
    • 近期文章
    • Laravel 10.13版本发布(0个评论)
    • 在github创建task的同时创建分支流程步骤(0个评论)
    • 在go语言中以邮件标题中获取SPF和DMARC,来判断是否为垃圾邮件之垃圾邮件过滤器功能实现(0个评论)
    • 在go语言中使用attr字段标签提取XML属性数据示例(0个评论)
    • 在laravel中介绍一个生成假数据的PHP库:FakerPHP(0个评论)
    • 在laravel框架中对环境配置文件的加载过程步骤浅析(0个评论)
    • Laravel 10.12版本发布(0个评论)
    • 在go语言中如何记录每个HTTP请求到你的Web服务器、日志记录器?(0个评论)
    • 在Go语言中如何查找一个IP地址的网络地址?(0个评论)
    • ELK + Filebeat 搭建日志系统流程步骤(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客