如何将重复计时器安排为5分钟间隔。它在00秒时触发,然后在00秒重复。好吧,不是硬实时的,但尽可能接近sys的延迟。试图避免积聚在滞后和接近00。
Lang: Python,操作系统: WinXP x64
系统分辨率为25ms。
任何代码都会有帮助的,tia
发布于 2010-08-24 11:31:59
我不知道如何做得比使用threading.Timer更准确。它是“一次性的”,但这只是意味着你以这种方式调度的函数必须立即重新调度自己,并在300秒后进行另一次调度。(您可以通过每次使用time.time测量准确的时间并相应地改变下一次调度延迟来提高准确性)。
发布于 2010-08-24 12:03:33
尝试比较这两个代码示例的时间打印输出:
代码示例1
import time
delay = 5
while True:
now = time.time()
print time.strftime("%H:%M:%S", time.localtime(now))
# As you will observe, this will take about 2 seconds,
# making the loop iterate every 5 + 2 seconds or so.
## repeat 5000 times
for i in range(5000):
sum(range(10000))
# This will sleep for 5 more seconds
time.sleep(delay)代码示例2
import time
delay = 5
while True:
now = time.time()
print time.strftime("%H:%M:%S", time.localtime(now))
# As you will observe, this will take about 2 seconds,
# but the loop will iterate every 5 seconds because code
# execution time was accounted for.
## repeat 5000 times
for i in range(5000):
sum(range(10000))
# This will sleep for as long as it takes to get to the
# next 5-second mark
time.sleep(delay - (time.time() - now))https://stackoverflow.com/questions/3553340
复制相似问题