Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-4347 Improve performance by only calling get_topology once #1673

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading