Skip to content

Commit e059fde

Browse files
authored
PYTHON-4347 [v4.8] Improve performance by only calling get_topology once (#1676)
1 parent 2fa651c commit e059fde

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pymongo/mongo_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ def __init__(
862862
server_monitoring_mode=options.server_monitoring_mode,
863863
)
864864

865+
self._opened = False
865866
self._init_background()
866867

867868
if connect:
@@ -1245,9 +1246,11 @@ def _get_topology(self) -> Topology:
12451246
If this client was created with "connect=False", calling _get_topology
12461247
launches the connection process in the background.
12471248
"""
1248-
self._topology.open()
1249-
with self.__lock:
1250-
self._kill_cursors_executor.open()
1249+
if not self._opened:
1250+
self._topology.open()
1251+
with self.__lock:
1252+
self._kill_cursors_executor.open()
1253+
self._opened = True
12511254
return self._topology
12521255

12531256
@contextlib.contextmanager

0 commit comments

Comments
 (0)