pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='123456', db=2)
r = redis.Redis(connection_pool=pool)
备注: 使用以上方法初始化连接池无法通过ssl参数启用ssl连接:
class ConnectionPool(object):
def __init__(self, connection_class=Connection, max_connections=None,
**connection_kwargs):
此处连接用了Connection。
如果需要使用ssl连接,则初始化连接池时使用from_url方法初始化连接池,参数格式如:
rediss://[:password]@localhost:6379/0 ,6379表示端口,0表示使用的数据库索引值。
pool = redis.ConnectionPool.from_url('rediss://:123456@localhost:6380/2')
r = redis.StrictRedis(connection_pool=pool)
ret = r.get('test')
pool.disconnect() //断开连接池的所有连接。