Python中池中的Redis默认连接数

Python中池中的Redis默认连接数,python,redis,Python,Redis,Python-3.7 Redis-2.10.6 我正在使用为Redis创建连接池 redis_pool = redis.ConnectionPool(host=REDIS_URL, port=REDIS_PORT, decode_responses=True) 我没有指定max\u connections。查看redis.ConnectionPool()的源代码时 def uuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

Python-3.7

Redis-2.10.6


我正在使用为Redis创建连接池

redis_pool = redis.ConnectionPool(host=REDIS_URL, port=REDIS_PORT, decode_responses=True) 
我没有指定
max\u connections
。查看redis.ConnectionPool()的源代码时

def uuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
**连接(kwargs):
"""
创建连接池。如果设置了max_connections,则
对象在达到池的限制时引发redis.ConnectionError。
默认情况下,创建TCP连接,除非选择connection_类
指定。请对unix套接字使用redis.UnixDomainSocketConnection。
任何其他关键字参数都会传递给的构造函数
连接到U类。
"""
最大连接数=最大连接数或2**31
如果不存在(最大连接数,(int,long))或最大连接数<0:
raise VALUERROR(“'max_connections”必须为正整数”)
self.connection\u class=连接\u class
self.connection\u kwargs=connection\u kwargs
self.max\u connections=max\u connections
self.reset()
我看到
max\u connections
设置为2**31,即2147483648(如果未设置)。这对我来说很奇怪


Redis在池中维护的默认连接数是多少?最大值约为200万。因此,这意味着我们必须传递我们自己的实际价值。

池在Redis端并不存在,这个类实际上只是Python端的
self.connection\u类
实例的奇特集合

不过,我同意你的看法,99%以上的时间2**31数字是不必要的巨大。不过,不要认为这太令人担忧,因为初始化池不会创建任何连接(或为它们保留空间)
max\u connections
仅限制
\u available\u connections
数组,当需要连接时,该数组会增长,但池中没有可立即使用的空闲连接

下面是关于
ConnectionPool
类的更多内容和一些注释

def重置(自):
self.pid=os.getpid()
自创建的连接=0

self._available_connections=[]。池在Redis端不存在,这个类实际上只是Python端的
self.connection_类
实例的奇特集合

不过,我同意你的看法,99%以上的时间2**31数字是不必要的巨大。不过,不要认为这太令人担忧,因为初始化池不会创建任何连接(或为它们保留空间)
max\u connections
仅限制
\u available\u connections
数组,当需要连接时,该数组会增长,但池中没有可立即使用的空闲连接

下面是关于
ConnectionPool
类的更多内容和一些注释

def重置(自):
self.pid=os.getpid()
自创建的连接=0
self._可用_连接=[]#
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()
def reset(self):
    self.pid = os.getpid()
    self._created_connections = 0
    self._available_connections = []  # <- starts empty
    self._in_use_connections = set()
    self._check_lock = threading.Lock()
def get_connection(self, command_name, *keys, **options):
    "Get a connection from the pool"
    self._checkpid()
    try:
        connection = self._available_connections.pop()
    except IndexError:
        connection = self.make_connection()  # <- make a new conn only if _available_connections is tapped
    self._in_use_connections.add(connection)
    try:
        # ensure this connection is connected to Redis
        connection.connect()
        # connections that the pool provides should be ready to send
        # a command. if not, the connection was either returned to the
        # pool before all data has been read or the socket has been
        # closed. either way, reconnect and verify everything is good.
        if not connection.is_ready_for_command():
            connection.disconnect()
            connection.connect()
            if not connection.is_ready_for_command():
                raise ConnectionError('Connection not ready')
    except:  # noqa: E722
        # release the connection back to the pool so that we don't leak it
        self.release(connection)
        raise

    return connection
 def make_connection(self):
    "Create a new connection"
    if self._created_connections >= self.max_connections:  # <- where the bounding happens
        raise ConnectionError("Too many connections")
    self._created_connections += 1
    return self.connection_class(**self.connection_kwargs)