Overview
En esta guía, puede aprender cómo PyMongo usa grupos de conexiones para administrar conexiones a una implementación de MongoDB y cómo puede configurar los ajustes del grupo de conexiones en su aplicación.
Un pool de conexiones es un caché de conexiones de base de datos abiertas que PyMongo mantiene. Cuando su aplicación solicita una conexión a MongoDB, PyMongo obtiene una conexión del pool sin problemas, realiza operaciones y la devuelve al pool para su reutilización.
Los grupos de conexiones ayudan a reducir la latencia de la aplicación y la cantidad de veces que PyMongo crea nuevas conexiones.
Configuración de grupos de conexiones
Puede especificar las siguientes configuraciones de grupo de conexiones en su MongoClient objeto o en su URI de conexión:
Configuración | Descripción |
|---|---|
| The time that PyMongo waits when establishing a new
connection before timing out. Data Type: intDefault: 20000MongoClient Example: connectTimeoutMS = 40000Connection URI Example: connectTimeoutMS=40000 |
| 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: intDefault: 2MongoClient Example: maxConnecting = 3Connection URI Example: maxConnecting=3 |
| 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: intDefault: None (no limit)MongoClient Example: maxIdleTimeMS = 60000Connection URI Example: maxIdleTimeMS=60000 |
| 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: intDefault: 100MongoClient Example: maxPoolSize = 150Connection URI Example: maxPoolSize=150 |
| 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: intDefault: 0MongoClient Example: minPoolSize = 3Connection URI Example: minPoolSize=3 |
| The length of time that PyMongo waits for a response from the server
before timing out. Data Type: intDefault: None (no timeout)MongoClient Example: socketTimeoutMS = 100000Connection URI Example: socketTimeoutMS=100000 |
| How long a thread waits for a connection to become available in the connection pool
before timing out. Data Type: intDefault: None (no timeout)MongoClient Example: waitQueueTimeoutMS = 100000Connection URI Example: waitQueueTimeoutMS=100000 |
El siguiente código crea un cliente con un tamaño máximo de grupo de conexiones de 50 mediante el parámetro maxPoolSize. Seleccione el Synchronous o 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 una URI de conexión:
client = MongoClient(host, port, maxPoolSize=50)
client = AsyncMongoClient(host, port, maxPoolSize=50)
Información Adicional
Para obtener más información sobre los grupos de conexiones, consulte Descripción general del grupo de conexiones en el manual del servidor MongoDB.
Documentación de la API
Para aprender más sobre cualquiera de los métodos o tipos analizados en esta guía, consulta la siguiente documentación de API: