cursor_manager.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright 2009-present 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. """DEPRECATED - A manager to handle when cursors are killed after they are
  15. closed.
  16. New cursor managers should be defined as subclasses of CursorManager and can be
  17. installed on a client by calling
  18. :meth:`~pymongo.mongo_client.MongoClient.set_cursor_manager`.
  19. .. versionchanged:: 3.3
  20. Deprecated, for real this time.
  21. .. versionchanged:: 3.0
  22. Undeprecated. :meth:`~pymongo.cursor_manager.CursorManager.close` now
  23. requires an `address` argument. The ``BatchCursorManager`` class is removed.
  24. """
  25. import warnings
  26. import weakref
  27. from bson.py3compat import integer_types
  28. class CursorManager(object):
  29. """DEPRECATED - The cursor manager base class."""
  30. def __init__(self, client):
  31. """Instantiate the manager.
  32. :Parameters:
  33. - `client`: a MongoClient
  34. """
  35. warnings.warn(
  36. "Cursor managers are deprecated.",
  37. DeprecationWarning,
  38. stacklevel=2)
  39. self.__client = weakref.ref(client)
  40. def close(self, cursor_id, address):
  41. """Kill a cursor.
  42. Raises TypeError if cursor_id is not an instance of (int, long).
  43. :Parameters:
  44. - `cursor_id`: cursor id to close
  45. - `address`: the cursor's server's (host, port) pair
  46. .. versionchanged:: 3.0
  47. Now requires an `address` argument.
  48. """
  49. if not isinstance(cursor_id, integer_types):
  50. raise TypeError("cursor_id must be an integer")
  51. self.__client().kill_cursors([cursor_id], address)