Menu Docs
Página inicial do Docs
/ / /
Driver Pymongo
/ /

Pool de Conexões

Neste guia, você aprenderá como o PyMongo usa pools de conexões para gerenciar conexões com uma MongoDB deployment e como definir as configurações do pool de conexões em seu aplicação.

Um pool de conexões é um cache de conexões de banco de dados abertas mantidas pelo PyMongo. Quando seu aplicação solicita uma conexão com o MongoDB, o PyMongo obtém uma conexão do pool sem problemas, executa operações e retorna a conexão ao pool para reutilização.

Os pools de conexões ajudam a reduzir a latência de aplicação e o número de vezes que novas conexões são criadas pelo PyMongo.

Você pode especificar as seguintes configurações do pool de conexões no objeto MongoClient ou no URI de conexão:

Contexto
Descrição

connectTimeoutMS

The time that PyMongo waits when establishing a new connection before timing out.

Data Type: int
Default: 20000
MongoClient Example: connectTimeoutMS = 40000
Connection URI Example: connectTimeoutMS=40000

maxConnecting

The maximum number of connections that each pool can establish concurrently. If this limit is reached, further requests wait until a connection is established or another in-use connection is checked back into the pool.

Data Type: int
Default: 2
MongoClient Example: maxConnecting = 3
Connection URI Example: maxConnecting=3

maxIdleTimeMS

The maximum time that a connection can remain idle in the pool. When a connection exceeds this limit, PyMongo closes the connection and removes it from the pool.

Data Type: int
Default: None (no limit)
MongoClient Example: maxIdleTimeMS = 60000
Connection URI Example: maxIdleTimeMS=60000

maxPoolSize

The maximum number of concurrent connections that the pool maintains. If the maximum pool size is reached, further requests wait until a connection becomes available.

Data Type: int
Default: 100
MongoClient Example: maxPoolSize = 150
Connection URI Example: maxPoolSize=150

minPoolSize

The minimum number of concurrent connections that the pool maintains. If the number of open connections falls below this value due to network errors, PyMongo attempts to create new connections to maintain this minimum.

Data Type: int
Default: 0
MongoClient Example: minPoolSize = 3
Connection URI Example: minPoolSize=3

socketTimeoutMS

The length of time that PyMongo waits for a response from the server before timing out.

Data Type: int
Default: None (no timeout)
MongoClient Example: socketTimeoutMS = 100000
Connection URI Example: socketTimeoutMS=100000

waitQueueTimeoutMS

How long a thread waits for a connection to become available in the connection pool before timing out.

Data Type: int
Default: None (no timeout)
MongoClient Example: waitQueueTimeoutMS = 100000
Connection URI Example: waitQueueTimeoutMS=100000

O código a seguir cria um cliente com um tamanho máximo do pool de conexões de 50 usando o parâmetro maxPoolSize. Selecione a aba Synchronous ou Asynchronous para ver o código correspondente:

client = MongoClient(host, port, maxPoolSize=50)
client = AsyncMongoClient(host, port, maxPoolSize=50)

O código a seguir cria um cliente com a mesma configuração do exemplo anterior, mas usa um URI de conexão:

client = MongoClient(host, port, maxPoolSize=50)
client = AsyncMongoClient(host, port, maxPoolSize=50)

Para saber mais sobre pools de conexões, consulte Visão geral do pool de conexões no manual do MongoDB Server .

Para saber mais sobre qualquer um dos métodos ou tipos discutidos neste guia, consulte a seguinte documentação da API:

Voltar

Limitar o tempo de execução do servidor MongoDB

Nesta página