我正在尝试在特定的时间间隔执行功能。 时间间隔是可动态设置的(在运行期间) 我正在使用线程来做到这一点。 我无法为该功能设置动态超时。
这是我的代码
from threading import Timer
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
self.start()
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
print(stopped)
timeout = 5
# This timeout should be changed during runtime of the thread
rf = RepeatedTimer(timeout, execute_Command)