cursor_manager.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright 2009-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """A manager to handle when cursors are killed after they are closed.
  15. New cursor managers should be defined as subclasses of CursorManager and can be
  16. installed on a client by calling
  17. :meth:`~pymongo.mongo_client.MongoClient.set_cursor_manager`.
  18. .. versionchanged:: 3.0
  19. Undeprecated. :meth:`~pymongo.cursor_manager.CursorManager.close` now
  20. requires an `address` argument. The ``BatchCursorManager`` class is removed.
  21. """
  22. import weakref
  23. from bson.py3compat import integer_types
  24. class CursorManager(object):
  25. """The cursor manager base class."""
  26. def __init__(self, client):
  27. """Instantiate the manager.
  28. :Parameters:
  29. - `client`: a MongoClient
  30. """
  31. self.__client = weakref.ref(client)
  32. def close(self, cursor_id, address):
  33. """Kill a cursor.
  34. Raises TypeError if cursor_id is not an instance of (int, long).
  35. :Parameters:
  36. - `cursor_id`: cursor id to close
  37. - `address`: the cursor's server's (host, port) pair
  38. .. versionchanged:: 3.0
  39. Now requires an `address` argument.
  40. """
  41. if not isinstance(cursor_id, integer_types):
  42. raise TypeError("cursor_id must be an integer")
  43. self.__client().kill_cursors([cursor_id], address)