图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.2 KiB

  1. """Kazoo Interfaces
  2. .. versionchanged:: 1.4
  3. The classes in this module used to be interface declarations based on
  4. `zope.interface.Interface`. They were converted to normal classes and
  5. now serve as documentation only.
  6. """
  7. # public API
  8. class IHandler(object):
  9. """A Callback Handler for Zookeeper completion and watch callbacks.
  10. This object must implement several methods responsible for
  11. determining how completion / watch callbacks are handled as well as
  12. the method for calling :class:`IAsyncResult` callback functions.
  13. These functions are used to abstract differences between a Python
  14. threading environment and asynchronous single-threaded environments
  15. like gevent. The minimum functionality needed for Kazoo to handle
  16. these differences is encompassed in this interface.
  17. The Handler should document how callbacks are called for:
  18. * Zookeeper completion events
  19. * Zookeeper watch events
  20. .. attribute:: name
  21. Human readable name of the Handler interface.
  22. .. attribute:: timeout_exception
  23. Exception class that should be thrown and captured if a
  24. result is not available within the given time.
  25. .. attribute:: sleep_func
  26. Appropriate sleep function that can be called with a single
  27. argument and sleep.
  28. """
  29. def start(self):
  30. """Start the handler, used for setting up the handler."""
  31. def stop(self):
  32. """Stop the handler. Should block until the handler is safely
  33. stopped."""
  34. def select(self):
  35. """A select method that implements Python's select.select
  36. API"""
  37. def socket(self):
  38. """A socket method that implements Python's socket.socket
  39. API"""
  40. def create_connection(self):
  41. """A socket method that implements Python's
  42. socket.create_connection API"""
  43. def event_object(self):
  44. """Return an appropriate object that implements Python's
  45. threading.Event API"""
  46. def lock_object(self):
  47. """Return an appropriate object that implements Python's
  48. threading.Lock API"""
  49. def rlock_object(self):
  50. """Return an appropriate object that implements Python's
  51. threading.RLock API"""
  52. def async_result(self):
  53. """Return an instance that conforms to the
  54. :class:`~IAsyncResult` interface appropriate for this
  55. handler"""
  56. def spawn(self, func, *args, **kwargs):
  57. """Spawn a function to run asynchronously
  58. :param args: args to call the function with.
  59. :param kwargs: keyword args to call the function with.
  60. This method should return immediately and execute the function
  61. with the provided args and kwargs in an asynchronous manner.
  62. """
  63. def dispatch_callback(self, callback):
  64. """Dispatch to the callback object
  65. :param callback: A :class:`~kazoo.protocol.states.Callback`
  66. object to be called.
  67. """
  68. class IAsyncResult(object):
  69. """An Async Result object that can be queried for a value that has
  70. been set asynchronously.
  71. This object is modeled on the ``gevent`` AsyncResult object.
  72. The implementation must account for the fact that the :meth:`set`
  73. and :meth:`set_exception` methods will be called from within the
  74. Zookeeper thread which may require extra care under asynchronous
  75. environments.
  76. .. attribute:: value
  77. Holds the value passed to :meth:`set` if :meth:`set` was
  78. called. Otherwise `None`.
  79. .. attribute:: exception
  80. Holds the exception instance passed to :meth:`set_exception`
  81. if :meth:`set_exception` was called. Otherwise `None`.
  82. """
  83. def ready(self):
  84. """Return `True` if and only if it holds a value or an
  85. exception"""
  86. def successful(self):
  87. """Return `True` if and only if it is ready and holds a
  88. value"""
  89. def set(self, value=None):
  90. """Store the value. Wake up the waiters.
  91. :param value: Value to store as the result.
  92. Any waiters blocking on :meth:`get` or :meth:`wait` are woken
  93. up. Sequential calls to :meth:`wait` and :meth:`get` will not
  94. block at all."""
  95. def set_exception(self, exception):
  96. """Store the exception. Wake up the waiters.
  97. :param exception: Exception to raise when fetching the value.
  98. Any waiters blocking on :meth:`get` or :meth:`wait` are woken
  99. up. Sequential calls to :meth:`wait` and :meth:`get` will not
  100. block at all."""
  101. def get(self, block=True, timeout=None):
  102. """Return the stored value or raise the exception
  103. :param block: Whether this method should block or return
  104. immediately.
  105. :type block: bool
  106. :param timeout: How long to wait for a value when `block` is
  107. `True`.
  108. :type timeout: float
  109. If this instance already holds a value / an exception, return /
  110. raise it immediately. Otherwise, block until :meth:`set` or
  111. :meth:`set_exception` has been called or until the optional
  112. timeout occurs."""
  113. def get_nowait(self):
  114. """Return the value or raise the exception without blocking.
  115. If nothing is available, raise the Timeout exception class on
  116. the associated :class:`IHandler` interface."""
  117. def wait(self, timeout=None):
  118. """Block until the instance is ready.
  119. :param timeout: How long to wait for a value when `block` is
  120. `True`.
  121. :type timeout: float
  122. If this instance already holds a value / an exception, return /
  123. raise it immediately. Otherwise, block until :meth:`set` or
  124. :meth:`set_exception` has been called or until the optional
  125. timeout occurs."""
  126. def rawlink(self, callback):
  127. """Register a callback to call when a value or an exception is
  128. set
  129. :param callback:
  130. A callback function to call after :meth:`set` or
  131. :meth:`set_exception` has been called. This function will
  132. be passed a single argument, this instance.
  133. :type callback: func
  134. """
  135. def unlink(self, callback):
  136. """Remove the callback set by :meth:`rawlink`
  137. :param callback: A callback function to remove.
  138. :type callback: func
  139. """