我想每 0.5 秒触发一个功能,并能够启动、停止和重置计时器。我不太了解 Python 线程的工作原理,并且在使用 python 计时器时遇到困难。
但是,当我执行 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()
原文由 user1431282 发布,翻译遵循 CC BY-SA 4.0 许可协议
最好的方法是启动定时器线程一次。在您的计时器线程中,您将编写以下代码
在启动计时器的代码中,您可以
set
停止事件来停止计时器。