개요
이 가이드 에서는 PyMongo 가 연결 풀을 사용하여 MongoDB deployment 에 대한 연결을 관리 방법과 애플리케이션 에서 연결 풀 설정을 구성하는 방법에 대해 학습 합니다.
연결 풀 은 PyMongo 에서 유지 관리하는 개방형 데이터베이스 연결의 캐시 입니다. 애플리케이션 이 MongoDB 에 대한 연결을 요청하면 PyMongo 풀에서 원활하게 연결을 가져오고, 작업을 수행하고, 재사용을 위해 풀에 연결을 반환합니다.
연결 풀은 애플리케이션 지연 시간 줄이고 PyMongo 에서 새 연결을 생성하는 횟수를 줄이는 데 도움이 됩니다.
연결 풀 구성
MongoClient 객체 또는 연결 URI에서 다음 연결 풀 설정을 지정할 수 있습니다.
설정 | 설명 |
|---|---|
| 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. 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 |
다음 코드는 maxPoolSize 매개변수를 사용하여 최대 연결 풀 크기가 50 인 클라이언트 만듭니다. Synchronous 또는 Asynchronous 탭 선택하여 해당 코드를 확인합니다.
client = MongoClient(host, port, maxPoolSize=50)
client = AsyncMongoClient(host, port, maxPoolSize=50)
다음 코드는 앞의 예시 와 구성이 동일하지만 연결 URI를 사용하여 클라이언트 생성합니다.
client = MongoClient(host, port, maxPoolSize=50)
client = AsyncMongoClient(host, port, maxPoolSize=50)
추가 정보
연결 풀에 대해 자세히 학습 MongoDB Server 매뉴얼의 연결풀 개요를 참조하세요.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.