我希望每隔0.5秒就可以触发一个函数,并且能够启动、停止和重置计时器。我不太了解Python线程是如何工作的,而且在使用Python定时器时遇到了困难。
然而,当我执行“threading.timer.start()”两次时,我一直收到“RuntimeError: threads can only be started once”的错误提示。是否有解决方法?我尝试在每次开始之前应用“threading.timer.cancel()”。
伪代码:
然而,当我执行“threading.timer.start()”两次时,我一直收到“RuntimeError: threads can only be started once”的错误提示。是否有解决方法?我尝试在每次开始之前应用“threading.timer.cancel()”。
伪代码:
t=threading.timer(0.5,function)
while True:
t.cancel()
t.start()