From e059fdef6bea233de1c4fa70c679f93597ba783e Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Wed, 12 Jun 2024 13:20:52 -0700 Subject: [PATCH] PYTHON-4347 [v4.8] Improve performance by only calling get_topology once (#1676) --- pymongo/mongo_client.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index 6cc1b2ced..ba14cbba2 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -862,6 +862,7 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]): server_monitoring_mode=options.server_monitoring_mode, ) + self._opened = False self._init_background() if connect: @@ -1245,9 +1246,11 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]): If this client was created with "connect=False", calling _get_topology launches the connection process in the background. """ - self._topology.open() - with self.__lock: - self._kill_cursors_executor.open() + if not self._opened: + self._topology.open() + with self.__lock: + self._kill_cursors_executor.open() + self._opened = True return self._topology @contextlib.contextmanager