使用 python:以 x:00 重复的间隔

发布于 2024-09-15 08:23:45 字数 160 浏览 5 评论 0原文

如何安排 5 分钟间隔的重复计时器。在 00 秒触发,然后在 00 秒重复。好吧,不是硬实时,但尽可能接近系统滞后。试图避免滞后累积并接近 00。Lang

:Python,操作系统:WinXP x64

系统具有 25 毫秒分辨率。

任何代码都会有帮助,蒂亚

How do I sched a repeat timer for 5 min intervals. Which fire at 00 seconds, then repeat at 00. Ok, not hard real-time but as close as possible with sys lags. Trying to avoid a build up in lags and get near 00.

Lang: Python, OS: WinXP x64

System has 25ms resolution.

Any code would be helpful, tia

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落叶缤纷 2024-09-22 08:23:45

I don't know how to do it any more accurately than with threading.Timer. It's "one-shot", but that just means the function you schedule that way must immediately re-schedule itself for another 300 seconds later, first thing. (You can add accuracy by measuring the exact time with time.time each time and varying the next scheduling delay accordingly).

吝吻 2024-09-22 08:23:45

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

代码示例 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))

Try and compare the time printouts of these two code samples:

Code Sample 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)

Code Sample 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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文