如何安排重复计时器5分钟间隔。在00秒钟发射什么,然后在00时重复。好的,不是实时的,而是在系统滞后时尽可能近。试图避免在滞后堆积并接近00。

Lang:Python,OS:WinxP X64

系统具有25毫秒的分辨率。

任何代码都会有所帮助,tia

有帮助吗?

解决方案

我不知道该如何准确地做 螺纹. 。它是“一声”,但这只是意味着您安排的功能必须立即重新安排本身,再过300秒,第一件事。 (您可以通过测量确切的时间来添加准确性 time.time 每次都相应地改变下一个调度延迟)。

其他提示

尝试比较这两个代码示例的时间打印输出:

代码样本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))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top