Details
-
New Feature
-
Status: Closed
-
Major - P3
-
Resolution: Won't Fix
-
None
-
None
-
None
-
Major Change
Description
In PyMongo 2.x, MongoClient and MongoReplicaSetClient have methods start_request and end_request. They are stateful and use a threadlocal to manage that state:
client.start_request()
|
# ... this thread is pinned to a socket ...
|
client.db.collection.insert(doc)
|
client.end_request()
|
In PyMongo 3, change the API to:
with client.start_request() as request:
|
request.db.collection.insert(doc)
|
A Request is a MongoClient that is pinned to a socket.
Some additional design questions remain:
- Does the Request class support the full MongoClient API?
- Are there any changes from the existing socket- and server-pinning behavior, especially regarding pinning to secondaries?
Justification:
PyMongo 2.x's threadlocal request state makes it hard to support async frameworks, and adds dire risk to the most critical portion of the driver: the connection pool. The pool code is filled with workarounds to threadlocal issues in Python 2.6 and in mod_wsgi. Only the most expert developers can make changes.
Requests are only useful with unacknowledged writes that use legacy opcodes. Thus requests are rarely, if ever, useful in modern applications. Therefor a change to the request API is a very small cost, but it comes with a big payoff: a simple and robust connection pool.