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

This commit is contained in:
Shane Harvey 2024-06-12 11:02:19 -07:00 committed by GitHub
parent 76f1221e22
commit 5dd6ffbbb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

View File

@ -854,6 +854,7 @@ class AsyncMongoClient(common.BaseObject, Generic[_DocumentType]):
server_monitoring_mode=options.server_monitoring_mode,
)
self._opened = False
self._init_background()
if _IS_SYNC and connect:
@ -1528,9 +1529,11 @@ class AsyncMongoClient(common.BaseObject, Generic[_DocumentType]):
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

View File

@ -853,6 +853,7 @@ class MongoClient(common.BaseObject, Generic[_DocumentType]):
server_monitoring_mode=options.server_monitoring_mode,
)
self._opened = False
self._init_background()
if _IS_SYNC and connect:
@ -1527,9 +1528,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