Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

Pools de conexiones

En esta guía, puedes aprender cómo PyMongo utiliza los pools de conexiones para gestionar las conexiones a una implementación de MongoDB y cómo puedes configurar los ajustes del pool de conexiones en tu aplicación.

Un pool de conexiones es una caché de conexiones de base de datos abiertas mantenidas por PyMongo. Cuando tu aplicación solicita una conexión a MongoDB, PyMongo obtiene de manera transparente una conexión del pool, realiza las operaciones y devuelve la conexión al pool para que se reutilice.

Las agrupaciones de conexiones ayudan a reducir la latencia de la aplicación y la cantidad de veces que PyMongo crea nuevas conexiones.

Puedes especificar los siguientes parámetros de pool de conexiones en tus MongoClient objeto o en su URI de conexión:

Configuración
Descripción

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. Set this value higher than your application's expected idle period but lower than firewall or proxy connection timeouts to prevent unexpected disconnections.

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

El siguiente código crea un cliente con un tamaño máximo de pool de conexiones de 50 utilizando el parámetro maxPoolSize. Selecciona el Synchronous o la pestaña Asynchronous para ver el código correspondiente:

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

El siguiente código crea un cliente con la misma configuración que el ejemplo anterior, pero utiliza un URI de conexión:

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

Para aprender más sobre los pools de conexión, consulta Descripción general del pool de conexiones en el manual del MongoDB Server.

Para aprender más sobre cualquiera de los métodos o tipos analizados en esta guía, consulta la siguiente documentación de API:

  • MongoClient

Volver

Limite el tiempo de ejecución del servidor

En esta página