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)
| 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. |