Python-3.7版
Redis-2.10.6版
我正在为Redis创建一个连接池
redis_pool = redis.ConnectionPool(host=REDIS_URL, port=REDIS_PORT, decode_responses=True)
我没有指定max_connections
。在查看redis.ConnectionPool()
的源代码时
def __init__(self, connection_class=Connection, max_connections=None,
**connection_kwargs):
"""
Create a connection pool. If max_connections is set, then this
object raises redis.ConnectionError when the pool's limit is reached.
By default, TCP connections are created unless connection_class is
specified. Use redis.UnixDomainSocketConnection for unix sockets.
Any additional keyword arguments are passed to the constructor of
connection_class.
"""
max_connections = max_connections or 2 ** 31
if not isinstance(max_connections, (int, long)) or max_connections < 0:
raise ValueError('"max_connections" must be a positive integer')
self.connection_class = connection_class
self.connection_kwargs = connection_kwargs
self.max_connections = max_connections
self.reset()
我看到max_connections
设置为2**31即2147483648(如果未设置)。我觉得很奇怪。你知道吗
Redis在池中维护的默认连接数是多少?最大值约为200万。所以,这意味着我们必须传递我们自己的实际价值。你知道吗
Redis端不存在这个池,这个类实际上只是Python端的
self.connection_class
实例的奇特集合。你知道吗不过,我同意你的看法,99%以上的时候2**31这个数字是不必要的巨大。不过,不要认为这太令人担心,因为初始化池不会创建任何连接(或为它们保留空间)。
max_connections
只限制_available_connections
数组,当需要连接时数组会增长,但池中没有可立即使用的空闲数组。你知道吗这里有更多的
ConnectionPool
类和一些注释。你知道吗https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L967
https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L983
https://github.com/andymccurdy/redis-py/blob/master/redis/connection.py#L1019
无论如何,我敢打赌,选择这个特定的值是为了将开发人员完全耗尽池的可能性降低到接近0。注意,连接对象是非常轻量级的,因此数千或数百万个连接对象的数组不太可能使应用程序停止运行。实际上,这并没有什么区别:大多数Redis调用返回得如此之快,以至于你很难意外地同时启动数百万个Redis调用。(如果你是故意这么做的,你可能知道的足够多,可以根据你的具体需要调整一切。;—)
相关问题 更多 >
编程相关推荐