Skip to content

Commit 5dd6ffb

Browse files
authored
PYTHON-4347 Improve performance by only calling get_topology once (mongodb#1673)
1 parent 76f1221 commit 5dd6ffb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

pymongo/asynchronous/mongo_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,7 @@ def __init__(
854854
server_monitoring_mode=options.server_monitoring_mode,
855855
)
856856

857+
self._opened = False
857858
self._init_background()
858859

859860
if _IS_SYNC and connect:
@@ -1528,9 +1529,11 @@ async def _get_topology(self) -> Topology:
15281529
If this client was created with "connect=False", calling _get_topology
15291530
launches the connection process in the background.
15301531
"""
1531-
await self._topology.open()
1532-
async with self._lock:
1533-
self._kill_cursors_executor.open()
1532+
if not self._opened:
1533+
await self._topology.open()
1534+
async with self._lock:
1535+
self._kill_cursors_executor.open()
1536+
self._opened = True
15341537
return self._topology
15351538

15361539
@contextlib.asynccontextmanager

pymongo/synchronous/mongo_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ def __init__(
853853
server_monitoring_mode=options.server_monitoring_mode,
854854
)
855855

856+
self._opened = False
856857
self._init_background()
857858

858859
if _IS_SYNC and connect:
@@ -1527,9 +1528,11 @@ def _get_topology(self) -> Topology:
15271528
If this client was created with "connect=False", calling _get_topology
15281529
launches the connection process in the background.
15291530
"""
1530-
self._topology.open()
1531-
with self._lock:
1532-
self._kill_cursors_executor.open()
1531+
if not self._opened:
1532+
self._topology.open()
1533+
with self._lock:
1534+
self._kill_cursors_executor.open()
1535+
self._opened = True
15331536
return self._topology
15341537

15351538
@contextlib.contextmanager

0 commit comments

Comments
 (0)