PERF-6638 Use a shared MongoClient across all worker threads - #41
Open
blink1073 wants to merge 3 commits into
Open
PERF-6638 Use a shared MongoClient across all worker threads#41blink1073 wants to merge 3 commits into
blink1073 wants to merge 3 commits into
Conversation
Switch the MongoDB driver from creating one MongoClient per worker to a single shared client managed at the class level. Workers now run as threads (ThreadPoolExecutor) instead of separate processes when the driver declares THREAD_SAFE=True, allowing genuine client sharing via PyMongo's built-in thread-safe connection pool. Other drivers are unaffected and continue to use multiprocessing.Pool.
blink1073
marked this pull request as ready for review
June 16, 2026 12:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Jira Ticket: PERF-6638
Whats Changed:
Use a shared
MongoClientacross all worker threads for the MongoDB driver instead of creating one client per worker process.THREAD_SAFE = Falsesentinel toAbstractDriver;MongodbDriveropts in withTHREAD_SAFE = TrueMongodbDriverstores the client at the class level with reference counting so it is created once and closed only after the last worker finishestpcc.pyusesThreadPoolExecutorinstead ofmultiprocessing.Poolwhen the driver is thread-safe; all other drivers continue using multiprocessing unchangedloaderFuncis preserved for multiprocessing drivers and skipped for thread-safe driversThreadPoolExecutorcannot run Python threads in true parallel and the shared-client approach provides no throughput benefit over multiprocessingPatch testing results:
Benchmarked on Atlas M40 (MongoDB 8.0, us-east-1), 50 warehouses, 50 clients, 60 s runs, 3 runs per variant, using Python 3.14.6 free-threaded (GIL disabled).
Three variants compared:
baseline(production + pymongo 4.17.0),PERF-6638(this PR + pymongo 4.17.0), andPERF-6638 + PYTHON-5272(this PR + pymongo 4.18.0.dev0 with TLS session caching).Related PRs: PYTHON-5272 Implement TLS session resumption for connections