| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
|
| 27 |
from threading import Thread, Event |
| 28 |
from time import time |
| 29 |
|
| 30 |
log = logging.getLogger(__name__) |
| 31 |
log.addHandler(logging.NullHandler()) |
| 32 |
|
| 33 |
|
| 34 |
class InterruptableEvent(Event): |
| 35 |
""" |
| 36 |
This subclass solves the problem indicated in bug |
| 37 |
https://bugs.python.org/issue35935 that prevents the |
| 38 |
wait of an Event to be interrupted by a KeyboardInterrupt. |
| 39 |
""" |
| 40 |
|
| 41 |
def wait(self, timeout=None): |
| 42 |
if timeout is None: |
| 43 |
while not super().wait(0.1): |
| 44 |
pass |
| 45 |
else: |
| 46 |
timeout_start = time() |
| 47 |
while not super().wait(0.1) and time() <= timeout_start + timeout: |
| 48 |
pass |
| 49 |
|
| 50 |
|
| 51 |
class StoppableThread(Thread): |
| 52 |
""" Base class for Threads which require the ability |
| 53 |
to be stopped by a thread-safe method call |
| 54 |
""" |
| 55 |
|
| 56 |
def __init__(self): |
| 57 |
super().__init__() |
| 58 |
self._should_stop = InterruptableEvent() |
| 59 |
self._should_stop.clear() |
| 60 |
|
| 61 |
def join(self, timeout=0): |
| 62 |
""" Joins the current thread and forces it to stop after |
| 63 |
the timeout if necessary |
| 64 |
|
| 65 |
:param timeout: Timeout duration in seconds |
| 66 |
""" |
| 67 |
self._should_stop.wait(timeout) |
| 68 |
if not self.should_stop(): |
| 69 |
self.stop() |
| 70 |
return super().join(0) |
| 71 |
|
| 72 |
def stop(self): |
| 73 |
self._should_stop.set() |
| 74 |
|
| 75 |
def should_stop(self): |
| 76 |
return self._should_stop.is_set() |
| 77 |
|
| 78 |
def __repr__(self): |
| 79 |
return "<{}(should_stop={})>".format( |
| 80 |
self.__class__.__name__, self.should_stop()) |