社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
今天在写zabbix storm job监控脚本的时候用到了python的redis模块,之前也有用过,但是没有过多的了解,今天看了下相关的api和源码,看到有ConnectionPool的实现,这里简单说下。
在ConnectionPool之前,如果需要连接redis,我都是用StrictRedis这个类,在源码中可以看到这个类的具体解释:
- redis.StrictRedis Implementation of the Redis protocol.This abstract class provides a Python interface to all Redis commands and an
- 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这个类之后,可以使用如下方法
- r=redis.StrictRedis(host=xxxx, port=xxxx, db=xxxx)
- r.xxxx()
这里Redis是StrictRedis的子类
简单分析如下:
在StrictRedis类的__init__方法中,可以初始化connection_pool这个参数,其对应的是一个ConnectionPool的对象: