Run a target function periodically on a background thread. If the target's return value is false, the executor stops. :param interval: Seconds between calls to `target`. :param min_interval: Minimum seconds between calls if `wake` is called very often. :
(
self,
interval: float,
min_interval: float,
target: Any,
name: Optional[str] = None,
)
| 122 | |
| 123 | class PeriodicExecutor: |
| 124 | def __init__( |
| 125 | self, |
| 126 | interval: float, |
| 127 | min_interval: float, |
| 128 | target: Any, |
| 129 | name: Optional[str] = None, |
| 130 | ): |
| 131 | """Run a target function periodically on a background thread. |
| 132 | |
| 133 | If the target's return value is false, the executor stops. |
| 134 | |
| 135 | :param interval: Seconds between calls to `target`. |
| 136 | :param min_interval: Minimum seconds between calls if `wake` is |
| 137 | called very often. |
| 138 | :param target: A function. |
| 139 | :param name: A name to give the underlying thread. |
| 140 | """ |
| 141 | # threading.Event and its internal condition variable are expensive |
| 142 | # in Python 2, see PYTHON-983. Use a boolean to know when to wake. |
| 143 | # The executor's design is constrained by several Python issues, see |
| 144 | # "periodic_executor.rst" in this repository. |
| 145 | self._event = False |
| 146 | self._interval = interval |
| 147 | self._min_interval = min_interval |
| 148 | self._target = target |
| 149 | self._stopped = False |
| 150 | self._thread: Optional[threading.Thread] = None |
| 151 | self._name = name |
| 152 | self._skip_sleep = False |
| 153 | self._thread_will_exit = False |
| 154 | self._lock = _create_lock() |
| 155 | |
| 156 | def __repr__(self) -> str: |
| 157 | return f"<{self.__class__.__name__}(name={self._name}) object at 0x{id(self):x}>" |
nothing calls this directly
no test coverage detected