Skip to content

Commit

Permalink
PYTHON-4347 Improve performance by only calling get_topology once (#1673
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ShaneHarvey committed Jun 12, 2024
1 parent 76f1221 commit 5dd6ffb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 6 additions & 3 deletions pymongo/asynchronous/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,7 @@ def __init__(
server_monitoring_mode=options.server_monitoring_mode,
)

self._opened = False
self._init_background()

if _IS_SYNC and connect:
Expand Down Expand Up @@ -1528,9 +1529,11 @@ async def _get_topology(self) -> Topology:
If this client was created with "connect=False", calling _get_topology
launches the connection process in the background.
"""
await self._topology.open()
async with self._lock:
self._kill_cursors_executor.open()
if not self._opened:
await self._topology.open()
async with self._lock:
self._kill_cursors_executor.open()
self._opened = True
return self._topology

@contextlib.asynccontextmanager
Expand Down
9 changes: 6 additions & 3 deletions pymongo/synchronous/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ def __init__(
server_monitoring_mode=options.server_monitoring_mode,
)

self._opened = False
self._init_background()

if _IS_SYNC and connect:
Expand Down Expand Up @@ -1527,9 +1528,11 @@ def _get_topology(self) -> Topology:
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
Expand Down

0 comments on commit 5dd6ffb

Please sign in to comment.