我试图每小时用模块“ schedule”安排任务。我的问题是我需要先运行任务,然后每小时重新运行一次。
此代码可以正常工作,但要等一小时才能首次运行
import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
while True:
schedule.run_pending()
我想避免这样做:
import schedule
import time
def job():
print("This happens immediately then every hour")
schedule.every().hour.do(job)
while i == 0:
job()
i = i+1
while i == 1:
schedule.run_pending()
理想情况下,具有这样的选项会很好:
schedule.run_pending_now()
可能最简单解决方案是立即运行它并对其进行调度,例如使用:
import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
job() # Runs now.
while True:
schedule.run_pending() # Runs every hour, starting one hour from now.