【博文推荐】Python redis链接建立实现分析 - Go语言中文社区

【博文推荐】Python redis链接建立实现分析


今天在写zabbix storm job监控脚本的时候用到了python的redis模块,之前也有用过,但是没有过多的了解,今天看了下相关的api和源码,看到有ConnectionPool的实现,这里简单说下。

在ConnectionPool之前,如果需要连接redis,我都是用StrictRedis这个类,在源码中可以看到这个类的具体解释:

  1. redis.StrictRedis Implementation of the Redis protocol.This abstract class provides a Python interface to all Redis commands and an  
  2. implementation of the Redis protocol.Connection and Pipeline derive from this, implementing how the commands are sent and received to the Redis server 

使用的方法:

1
2
 r=redis.StrictRedis(host=xxxx, port=xxxx, db=xxxx)
 r.xxxx()

有了ConnectionPool这个类之后,可以使用如下方法

  1. r=redis.StrictRedis(host=xxxx, port=xxxx, db=xxxx) 
  2. r.xxxx() 

这里Redis是StrictRedis的子类

简单分析如下:

在StrictRedis类的__init__方法中,可以初始化connection_pool这个参数,其对应的是一个ConnectionPool的对象:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class StrictRedis(object):
........
    def __init__(self, host='localhost', port=6379,
                 db=0, password=None, socket_timeout=None,
                 socket_connect_timeout=None,
                 socket_keepalive=None, socket_keepalive_options 版权声明:本文来源51CTO,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:http://developer.51cto.com/art/201412/458583.htm
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-05-16 03:41:09
  • 阅读 ( 1206 )
  • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢