periodic_executor.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright 2014-present MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # 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
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. """Run a target function on a background thread."""
  15. import threading
  16. import time
  17. import weakref
  18. from pymongo.monotonic import time as _time
  19. class PeriodicExecutor(object):
  20. def __init__(self, interval, min_interval, target, name=None):
  21. """"Run a target function periodically on a background thread.
  22. If the target's return value is false, the executor stops.
  23. :Parameters:
  24. - `interval`: Seconds between calls to `target`.
  25. - `min_interval`: Minimum seconds between calls if `wake` is
  26. called very often.
  27. - `target`: A function.
  28. - `name`: A name to give the underlying thread.
  29. """
  30. # threading.Event and its internal condition variable are expensive
  31. # in Python 2, see PYTHON-983. Use a boolean to know when to wake.
  32. # The executor's design is constrained by several Python issues, see
  33. # "periodic_executor.rst" in this repository.
  34. self._event = False
  35. self._interval = interval
  36. self._min_interval = min_interval
  37. self._target = target
  38. self._stopped = False
  39. self._thread = None
  40. self._name = name
  41. self._skip_sleep = False
  42. self._thread_will_exit = False
  43. self._lock = threading.Lock()
  44. def __repr__(self):
  45. return '<%s(name=%s) object at 0x%x>' % (
  46. self.__class__.__name__, self._name, id(self))
  47. def open(self):
  48. """Start. Multiple calls have no effect.
  49. Not safe to call from multiple threads at once.
  50. """
  51. with self._lock:
  52. if self._thread_will_exit:
  53. # If the background thread has read self._stopped as True
  54. # there is a chance that it has not yet exited. The call to
  55. # join should not block indefinitely because there is no
  56. # other work done outside the while loop in self._run.
  57. try:
  58. self._thread.join()
  59. except ReferenceError:
  60. # Thread terminated.
  61. pass
  62. self._thread_will_exit = False
  63. self._stopped = False
  64. started = False
  65. try:
  66. started = self._thread and self._thread.is_alive()
  67. except ReferenceError:
  68. # Thread terminated.
  69. pass
  70. if not started:
  71. thread = threading.Thread(target=self._run, name=self._name)
  72. thread.daemon = True
  73. self._thread = weakref.proxy(thread)
  74. _register_executor(self)
  75. thread.start()
  76. def close(self, dummy=None):
  77. """Stop. To restart, call open().
  78. The dummy parameter allows an executor's close method to be a weakref
  79. callback; see monitor.py.
  80. """
  81. self._stopped = True
  82. def join(self, timeout=None):
  83. if self._thread is not None:
  84. try:
  85. self._thread.join(timeout)
  86. except (ReferenceError, RuntimeError):
  87. # Thread already terminated, or not yet started.
  88. pass
  89. def wake(self):
  90. """Execute the target function soon."""
  91. self._event = True
  92. def update_interval(self, new_interval):
  93. self._interval = new_interval
  94. def skip_sleep(self):
  95. self._skip_sleep = True
  96. def __should_stop(self):
  97. with self._lock:
  98. if self._stopped:
  99. self._thread_will_exit = True
  100. return True
  101. return False
  102. def _run(self):
  103. while not self.__should_stop():
  104. try:
  105. if not self._target():
  106. self._stopped = True
  107. break
  108. except:
  109. with self._lock:
  110. self._stopped = True
  111. self._thread_will_exit = True
  112. raise
  113. if self._skip_sleep:
  114. self._skip_sleep = False
  115. else:
  116. deadline = _time() + self._interval
  117. while not self._stopped and _time() < deadline:
  118. time.sleep(self._min_interval)
  119. if self._event:
  120. break # Early wake.
  121. self._event = False
  122. # _EXECUTORS has a weakref to each running PeriodicExecutor. Once started,
  123. # an executor is kept alive by a strong reference from its thread and perhaps
  124. # from other objects. When the thread dies and all other referrers are freed,
  125. # the executor is freed and removed from _EXECUTORS. If any threads are
  126. # running when the interpreter begins to shut down, we try to halt and join
  127. # them to avoid spurious errors.
  128. _EXECUTORS = set()
  129. def _register_executor(executor):
  130. ref = weakref.ref(executor, _on_executor_deleted)
  131. _EXECUTORS.add(ref)
  132. def _on_executor_deleted(ref):
  133. _EXECUTORS.remove(ref)
  134. def _shutdown_executors():
  135. if _EXECUTORS is None:
  136. return
  137. # Copy the set. Stopping threads has the side effect of removing executors.
  138. executors = list(_EXECUTORS)
  139. # First signal all executors to close...
  140. for ref in executors:
  141. executor = ref()
  142. if executor:
  143. executor.close()
  144. # ...then try to join them.
  145. for ref in executors:
  146. executor = ref()
  147. if executor:
  148. executor.join(1)
  149. executor = None