简体   繁体   English

For Loop 每 5 分钟执行一次,持续 24 小时

[英]For Loop executed every 5 minutes for 24 hours

I have a for loop and I want each iteration to be executed every 5 minutes.我有一个 for 循环,我希望每 5 分钟执行一次迭代。 Essentially, I want to freeze/sleep the loop for 5 minutes and then continue from where it left 5 minutes ago, NOT to start from the beginning.本质上,我想将循环冻结/休眠 5 分钟,然后从 5 分钟前离开的地方继续,而不是从头开始。 In total, I want to do this for an entire day (24hours).总的来说,我想这样做一整天(24 小时)。

You could define a method that simply calls sleep for 5 minutes and then call that in your loop.您可以定义一个简单地调用 sleep 5 分钟的方法,然后在循环中调用它。 Kind of like this有点像这样

import time 

# main loop
for i in .....:
   # do your stuff
   wait5Mins()
   # continue doing stuff

def wait5Mins():
    time.sleep(300)

Something like this?像这样的东西? The loop runs for (a little more than) twenty-four hours and sleeps for five minutes after each iteration.循环运行(多一点)二十四小时,每次迭代后休眠五分钟。

from time import sleep, time

ONE_DAY = 24 * 60 * 60  # seconds
FIVE_MINUTES = 5 * 60  # seconds


start_time = time()

current_time = start_time
while current_time <= start_time + ONE_DAY - FIVE MINUTES:
    # Do something loopy here...

    time.sleep(FIVE_MINUTES)
    current_time = time()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM