簡體   English   中英

如何從上午 9:15 到下午 3:30 的特定時間每 5 分鍾運行一次代碼?

[英]How to run a code after every 5 minutes from a specific time from 9:15 am to 3:30 pm?

我想每 5 分鍾運行一次腳本,但是在我運行它的時候,我希望它等待可整除的 5 分鍾。 例如,當前時間是上午 11:32:15,所以我第一次希望它從 11:35 運行,然后每 5 分鍾運行一次,直到下午 3:15

使用time.sleep 5 分鍾后運行代碼和datetime.datetime來獲取時間

這是代碼:

from datetime import datetime
import time
while True:
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    if current_time=="11:35:15":
        # YOUR CODE HERE
        time.sleep(300)
    elif current_time=="3:15:06":
        break

如果你想在休息前運行 1 次,你可以增加 current_time

您可以使用 windows 中的“任務調度程序”或 linux 中的“crontab”來調度腳本。

您可以使用schedule package,它對於此類任務非常靈活。

import schedule
import time

def job():
    print("I'm working...")

schedule.every(5).minute.until("2030-01-01 03:15").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM