periodic_executor.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2014-2015 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 atexit
  16. import threading
  17. import time
  18. import weakref
  19. from pymongo import thread_util
  20. from pymongo.monotonic import time as _time
  21. class PeriodicExecutor(object):
  22. def __init__(self, condition_class, interval, min_interval, target):
  23. """"Run a target function periodically on a background thread.
  24. If the target's return value is false, the executor stops.
  25. :Parameters:
  26. - `condition_class`: A class like threading.Condition.
  27. - `interval`: Seconds between calls to `target`.
  28. - `min_interval`: Minimum seconds between calls if `wake` is
  29. called very often.
  30. - `target`: A function.
  31. """
  32. self._event = thread_util.Event(condition_class)
  33. self._interval = interval
  34. self._min_interval = min_interval
  35. self._target = target
  36. self._stopped = False
  37. self._thread = None
  38. def open(self):
  39. """Start. Multiple calls have no effect.
  40. Not safe to call from multiple threads at once.
  41. """
  42. self._stopped = False
  43. started = False
  44. try:
  45. started = self._thread and self._thread.is_alive()
  46. except ReferenceError:
  47. # Thread terminated.
  48. pass
  49. if not started:
  50. thread = threading.Thread(target=self._run)
  51. thread.daemon = True
  52. self._thread = weakref.proxy(thread)
  53. _register_executor(self)
  54. thread.start()
  55. def close(self, dummy=None):
  56. """Stop. To restart, call open().
  57. The dummy parameter allows an executor's close method to be a weakref
  58. callback; see monitor.py.
  59. Since this can be called from a weakref callback during garbage
  60. collection it must take no locks!
  61. """
  62. self._stopped = True
  63. def join(self, timeout=None):
  64. if self._thread is not None:
  65. try:
  66. self._thread.join(timeout)
  67. except ReferenceError:
  68. # Thread already terminated.
  69. pass
  70. def wake(self):
  71. """Execute the target function soon."""
  72. self._event.set()
  73. def _run(self):
  74. while not self._stopped:
  75. try:
  76. if not self._target():
  77. self._stopped = True
  78. break
  79. except:
  80. self._stopped = True
  81. raise
  82. deadline = _time() + self._interval
  83. # Avoid running too frequently if wake() is called very often.
  84. time.sleep(self._min_interval)
  85. # Until the deadline, wake often to check if close() was called.
  86. while not self._stopped and _time() < deadline:
  87. # Our Event's wait returns True if set, else False.
  88. if self._event.wait(0.1):
  89. # Someone called wake().
  90. break
  91. self._event.clear()
  92. # _EXECUTORS has a weakref to each running PeriodicExecutor. Once started,
  93. # an executor is kept alive by a strong reference from its thread and perhaps
  94. # from other objects. When the thread dies and all other referrers are freed,
  95. # the executor is freed and removed from _EXECUTORS. If any threads are
  96. # running when the interpreter begins to shut down, we try to halt and join
  97. # them to avoid spurious errors.
  98. _EXECUTORS = set()
  99. def _register_executor(executor):
  100. ref = weakref.ref(executor, _on_executor_deleted)
  101. _EXECUTORS.add(ref)
  102. def _on_executor_deleted(ref):
  103. _EXECUTORS.remove(ref)
  104. def _shutdown_executors():
  105. # Copy the set. Stopping threads has the side effect of removing executors.
  106. executors = list(_EXECUTORS)
  107. for ref in executors:
  108. executor = ref()
  109. if executor:
  110. executor.close()
  111. executor.join(10)
  112. atexit.register(_shutdown_executors)