Python计划未按计划运行
我使用下面的代码每5分钟执行一次python脚本,但当它下次执行时,它不会像以前一样在执行时执行。 例如,如果我在上午9:00:00执行它,那么下次它将在上午9:05:25执行,下次将在上午9:10:45执行。由于我长时间每5分钟运行一次python脚本,因此无法准确记录时间。 进口时间表 导入时间 从日期时间导入日期时间Python计划未按计划运行,python,schedule,Python,Schedule,我使用下面的代码每5分钟执行一次python脚本,但当它下次执行时,它不会像以前一样在执行时执行。 例如,如果我在上午9:00:00执行它,那么下次它将在上午9:05:25执行,下次将在上午9:10:45执行。由于我长时间每5分钟运行一次python脚本,因此无法准确记录时间。 进口时间表 导入时间 从日期时间导入日期时间 # Functions setup def geeks(): print("Shaurya says Geeksforgeeks&quo
# Functions setup
def geeks():
print("Shaurya says Geeksforgeeks")
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
# Task scheduling
# After every 10mins geeks() is called.
schedule.every(2).minutes.do(geeks)
# Loop so that the scheduling task
# keeps on running all time.
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)
是否有任何简单的修复方法,以便下次脚本正好在5分钟内运行。
请不要建议我使用crontab,因为我已经尝试过crontab,但它不适合我。
我在不同的操作系统中使用python脚本你的极客函数将花费时间来执行,并在极客完成后安排作业开始计算5分钟,这就是为什么很长时间它无法在准确的时间记录。 如果希望函数在准确的时间运行,可以尝试以下操作:
# After every 10mins geeks() is called.
#schedule.every(2).minutes.do(geeks)
for _ in range(0,60,5):
schedule.every().hour.at(":"+str(_).zfill(2)).do(geeks)
# Loop so that the scheduling task
我想现在回答已经太迟了
import time
from datetime import datetime
from simple_scheduler.recurring import recurring_scheduler
recurring_scheduler.add_job(target=geeks, period_in_seconds=5*60)
recurring_scheduler.run()