This is a helper class to track the timeout of something.
| 158 | |
| 159 | |
| 160 | class TimeoutTracker(object): |
| 161 | """ |
| 162 | This is a helper class to track the timeout of something. |
| 163 | """ |
| 164 | |
| 165 | def __init__(self, py_db): |
| 166 | self._thread = None |
| 167 | self._lock = ThreadingLock() |
| 168 | self._py_db = weakref.ref(py_db) |
| 169 | |
| 170 | def call_on_timeout(self, timeout, on_timeout, kwargs=None): |
| 171 | """ |
| 172 | This can be called regularly to always execute the given function after a given timeout: |
| 173 | |
| 174 | call_on_timeout(py_db, 10, on_timeout) |
| 175 | |
| 176 | |
| 177 | Or as a context manager to stop the method from being called if it finishes before the timeout |
| 178 | elapses: |
| 179 | |
| 180 | with call_on_timeout(py_db, 10, on_timeout): |
| 181 | ... |
| 182 | |
| 183 | Note: the callback will be called from a PyDBDaemonThread. |
| 184 | """ |
| 185 | with self._lock: |
| 186 | if self._thread is None: |
| 187 | if _DEBUG: |
| 188 | pydev_log.critical("pydevd_timeout: Created _TimeoutThread.") |
| 189 | |
| 190 | self._thread = _TimeoutThread(self._py_db()) |
| 191 | self._thread.start() |
| 192 | |
| 193 | curtime = time.time() |
| 194 | handle = _OnTimeoutHandle(self, curtime + timeout, on_timeout, kwargs) |
| 195 | if _DEBUG: |
| 196 | pydev_log.critical("pydevd_timeout: Added handle: %s.", handle) |
| 197 | self._thread.add_on_timeout_handle(handle) |
| 198 | return handle |
| 199 | |
| 200 | |
| 201 | def create_interrupt_this_thread_callback(): |