@@ -17,14 +17,60 @@ How does connection pooling work in PyMongo?
1717Every :class: `~pymongo.mongo_client.MongoClient ` instance has a built-in
1818connection pool. The pool begins with one open connection. If necessary to
1919support concurrent access to MongoDB from multiple threads in your application,
20- the client will open new connections on demand.
20+ the client opens new connections on demand.
2121
2222By default, there is no thread-affinity for connections.
2323
24- The size of the connection pool is capped at ``max_pool_size `` (default 100).
25- When a thread in your application begins an operation on MongoDB, if no
26- connections are available and the pool has reach its maximum, the thread
27- blocks waiting for a connection to be returned to the pool by another thread.
24+ In versions before 2.6, the default ``max_pool_size `` was 10, and it did not
25+ actually bound the number of open connections; it only determined the number
26+ of connections that would be kept open when no longer in use.
27+
28+ Starting with PyMongo 2.6, the size of the connection pool is capped at
29+ ``max_pool_size ``, which now defaults to 100. When a thread in your application
30+ begins an operation on MongoDB, if all other connections are in use and the
31+ pool has reached its maximum, the thread pauses, waiting for a connection to
32+ be returned to the pool by another thread.
33+
34+ The default configuration for a :class: `~pymongo.mongo_client.MongoClient `
35+ works for most applications::
36+
37+ client = MongoClient(host, port)
38+
39+ Create this client **once ** when your program starts up, and reuse it for all
40+ operations. It is a common mistake to create a new client for each request,
41+ which is very inefficient.
42+
43+ To support extremely high numbers of concurrent MongoDB operations within one
44+ process, increase ``max_pool_size ``::
45+
46+ client = MongoClient(host, port, max_pool_size=200)
47+
48+ ... or make it unbounded::
49+
50+ client = MongoClient(host, port, max_pool_size=None)
51+
52+ By default, any number of threads are allowed to wait for connections to become
53+ available, and they can wait any length of time. Override ``waitQueueMultiple ``
54+ to cap the number of waiting threads. E.g., to keep the number of waiters less
55+ than or equal to 500::
56+
57+ client = MongoClient(host, port, max_pool_size=50, waitQueueMultiple=10)
58+
59+ When 500 threads are waiting for a socket, the 501st that needs a connection
60+ raises :exc: `~pymongo.errors.ExceededMaxWaiters `. Use this option to
61+ bound the amount of queueing in your application during a load spike, at the
62+ cost of additional exceptions.
63+
64+ Once the pool reaches its max size, additional threads are allowed to wait
65+ indefinitely for connections to become available, unless you set
66+ ``waitQueueTimeoutMS ``::
67+
68+ client = MongoClient(host, port, waitQueueTimeoutMS=100)
69+
70+ A thread that waits more than 100ms (in this example) for a connection raises
71+ :exc: `~pymongo.errors.ConnectionFailure `. Use this option if it is more
72+ important to bound the duration of operations during a load spike than it is to
73+ complete every operation.
2874
2975When :meth: `~pymongo.mongo_client.MongoClient.disconnect ` is called by any thread,
3076all sockets are closed.
0 commit comments