Docs Menu

연결 풀

이 가이드 에서는 PyMongo 가 연결 풀을 사용하여 MongoDB deployment 에 대한 연결을 관리 방법과 애플리케이션 에서 연결 풀 설정을 구성하는 방법에 대해 학습 합니다.

연결 풀 은 PyMongo 에서 유지 관리하는 개방형 데이터베이스 연결의 캐시 입니다. 애플리케이션 이 MongoDB 에 대한 연결을 요청하면 PyMongo 풀에서 원활하게 연결을 가져오고, 작업을 수행하고, 재사용을 위해 풀에 연결을 반환합니다.

연결 풀은 애플리케이션 지연 시간 줄이고 PyMongo 에서 새 연결을 생성하는 횟수를 줄이는 데 도움이 됩니다.

MongoClient 객체 또는 연결 URI에서 다음 연결 풀 설정을 지정할 수 있습니다.

설정
설명

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

다음 코드는 maxPoolSize 매개변수를 사용하여 최대 연결 풀 크기가 50 인 클라이언트 만듭니다.

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

다음 코드는 앞의 예시 와 구성이 동일하지만 연결 URI를 사용하여 클라이언트 생성합니다.

client = MongoClient("mongodb://<host>:<port>/?maxPoolSize=50")

연결 풀에 대해 자세히 학습 MongoDB Server 매뉴얼의 연결풀 개요를 참조하세요.

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.