我想每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()
来自Equivalent of setInterval in python:
用法:
或者这里是the same functionality but as a standalone function instead of a decorator:
Here's how to do it without using threads。
最好的方法是启动计时器线程一次。在你的计时器线程中,你可以编写以下代码
在启动计时器的代码中,然后可以
set
停止事件来停止计时器。使用计时器线程-
这可以通过
t.cancel()
停止相关问题 更多 >
编程相关推荐