睡一个python循环以在确切的时间迭代

时间:2015-12-05 03:44:30

标签: python loops datetime time

我有一个Python 3脚本,可以24/7运行无限循环。它建立了一个网站连接并转到某个网页下载文件。

每次迭代大约需要一分钟,但我需要脚本在相隔约5分钟的确切时间开始每次迭代。例如:小时后1分钟,小时后6分钟,小时后11分钟等等。我需要脚本在迭代之间休眠,直到再次开始的确切时间。只是想知道是否有一个干净的方法来做到这一点?

编辑:根据目前为止给出的答案,我想我需要重新说出我的问题。基本上,我们需要的是计算最后一次迭代结束的时间和我希望下一次迭代开始的时间。换句话说,我需要现在,当前时间和下一次分钟的时间差为01,06,11,16,21,26,31,36,41,46,51,56和时间的秒数是00.然后我可以在一段时间内对脚本进行time.sleep()。

2 个答案:

答案 0 :(得分:0)

查看请求库以进行Web连接,并将脚本放在cron作业中,让它每5分钟运行一次。

答案 1 :(得分:0)

嗯,我相信他们称之为" kludge"。我发布了下面的一些评论和打印行,以便澄清,看看它是如何工作的。无论如何,这似乎运作得很好。每个循环将在?1:30和?6:30之间开始(除了第一个循环),相隔5分钟,并且在循环开始时的几毫秒内。当然,如果您决定使用它,则需要进行编辑,但也许这是一个有用的开始。

##### import modules
import time
import datetime
from datetime import datetime
from datetime import timedelta
##### create list of possible loop start times; this here creates start times every 5 minutes, starting the loops at 01:30, 06:30, 11:30, etc., after the hour
dt_cur = datetime.now()
dt_4_list = datetime.strptime(str(dt_cur.year) + '-' + str(dt_cur.month) + '-' + str(dt_cur.day) + ' ' + str(dt_cur.hour) + ':1:30', '%Y-%m-%d %H:%M:%S')
ls_dt = [dt_4_list]
for i in range(288):
    dt_4_list = dt_4_list + timedelta(minutes = 5)
    ls_dt.append(dt_4_list)
##### begin the loop
loop_timer = 0
while loop_timer < 275:
    if loop_timer > 0:
        ##### check if the loop started before the exact time due to time.sleep() not being exactly accurate
        time_check = datetime.now().strftime("%S.%f")
        time_check = float(time_check)
        loop_time_check = 0
        while time_check < 30 and loop_time_check < 100:
            time_check = datetime.now().strftime("%S.%f")
            time_check = float(time_check)
            print(time_check, '- WARNING: time_check < 30; loop =', loop_time_check + 1, flush=True)
            time.sleep(.025)
            loop_time_check += 1
            if loop_time_check == 100:
                print(time_check, '- WARNING: exiting script early; time_check < 30 100 times', flush=True)
                raise SystemExit()
    ########################################################################################################################
    ##### start doing  stuff here ##########################################################################################
    dt_cur = datetime.now()
    print(dt_cur, flush=True)
    for i in range(5):          ##### this is where your own script does it's work before resting until the next loop
        print(i, flush=True)
        time.sleep(1)
    ##### end doing stuff here #############################################################################################
    ########################################################################################################################
    ##### create the current datetime var
    dt_cur = datetime.now()
    print(dt_cur, flush=True)
    ##### create the dt_next_loop time var, increment the loop
    for d in ls_dt:
        if d > dt_cur:
            dt_next_loop = d
            break
    loop_timer += 1
    ##### get the seconds to sleep the script but make it about 5 seconds short of the actual time to begin the next loop
    sleep_secs = (dt_next_loop - dt_cur) - timedelta(seconds=5)
    sleep_secs = timedelta.total_seconds(sleep_secs)
    time.sleep(sleep_secs)
    ##### sleep the script about 5 seconds more before the actual time to begin the next loop (sleep.time() is not precisely accurate
    ##### over minutes and doing this again 5 seconds before the time to begin the next loop will ensure more accuracy)
    sleep_secs = datetime.now().strftime("%S.%f")
    sleep_secs = 30 - float(sleep_secs)
    time.sleep(sleep_secs)