code: def close(self):
          
          session_ids = self._topology.pop_all_sessions()
          if session_ids:
              self._end_sessions(session_ids)
          # Stop the periodic task thread and then run _process_periodic_tasks
          # to send pending killCursor requests before closing the topology.
          self._kill_cursors_executor.close()
          self._process_periodic_tasks()
          self._topology.close()
docs: Cleanup client resources and disconnect from MongoDB.
  
          On MongoDB >= 3.6, 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.
          If this instance is used again it will be automatically re-opened and
          the threads restarted.
  
          .. versionchanged:: 3.6
             End all server sessions created by this client.