MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / close

Method close

pymongo/synchronous/mongo_client.py:1721–1755  ·  view source on GitHub ↗

Cleanup client resources and disconnect from MongoDB. End all server sessions created by this client by sending one or more endSessions commands. Close all sockets in the connection pools and stop the monitor threads. .. versionchanged:: 4.0 Once closed,

(self)

Source from the content-addressed store, hash-verified

1719 pass
1720
1721 def close(self) -> None:
1722 """Cleanup client resources and disconnect from MongoDB.
1723
1724 End all server sessions created by this client by sending one or more
1725 endSessions commands.
1726
1727 Close all sockets in the connection pools and stop the monitor threads.
1728
1729 .. versionchanged:: 4.0
1730 Once closed, the client cannot be used again and any attempt will
1731 raise :exc:`~pymongo.errors.InvalidOperation`.
1732
1733 .. versionchanged:: 3.6
1734 End all server sessions created by this client.
1735 """
1736 if self._topology is None:
1737 return
1738 session_ids = self._topology.pop_all_sessions()
1739 if session_ids:
1740 self._end_sessions(session_ids)
1741 # Stop the periodic task thread and then send pending killCursor
1742 # requests before closing the topology.
1743 self._kill_cursors_executor.close()
1744 self._process_kill_cursors()
1745 self._topology.close()
1746 if self._encrypter:
1747 # TODO: PYTHON-1921 Encrypted MongoClients cannot be re-opened.
1748 self._encrypter.close()
1749 self._closed = True
1750 if not _IS_SYNC:
1751 asyncio.gather(
1752 self._topology.cleanup_monitors(), # type: ignore[func-returns-value]
1753 self._kill_cursors_executor.join(), # type: ignore[func-returns-value]
1754 return_exceptions=True,
1755 )
1756
1757 if not _IS_SYNC:
1758 # Add support for contextlib.closing.

Callers 12

_run_scenarioFunction · 0.95
fail_pointMethod · 0.95
_connectMethod · 0.95
_check_user_providedMethod · 0.95
test_repr_srv_hostMethod · 0.95
mainFunction · 0.95
_connectFunction · 0.95
__exit__Method · 0.95
_cleanup_cursor_lockMethod · 0.45

Calls 5

_end_sessionsMethod · 0.95
_process_kill_cursorsMethod · 0.95
pop_all_sessionsMethod · 0.45
cleanup_monitorsMethod · 0.45
joinMethod · 0.45

Tested by 7

_run_scenarioFunction · 0.76
fail_pointMethod · 0.76
test_repr_srv_hostMethod · 0.76
_connectFunction · 0.76