Python 未如期运行 安排
Python schedule not running as scheduled
我使用下面的代码每 5 分钟执行一次 python 脚本,但是当它下次执行时,它不会像以前那样在准确的时间执行。
例如,如果我恰好在 9:00:00 AM 执行它,下一次它在 9:05:25 AM 执行,下一次在 9:10:45 AM 执行。因为我 运行 python 脚本很长一段时间内每 5 分钟一次,因此无法准确记录时间。
导入时间表
导入时间
从日期时间导入日期时间
# 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 但对我不起作用。
我在不同的 os
中使用 python 脚本
你的 geeks 函数将花费时间来执行,并且计划作业在 geeks 完成后 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
因为schedule
does not account for the time it takes for the job function to execute。请改用 ischedule
。以下内容适用于您的任务。
import ischedule
ischedule.schedule(geeks, interval=2*60)
ischedule.run_loop()
我使用下面的代码每 5 分钟执行一次 python 脚本,但是当它下次执行时,它不会像以前那样在准确的时间执行。 例如,如果我恰好在 9:00:00 AM 执行它,下一次它在 9:05:25 AM 执行,下一次在 9:10:45 AM 执行。因为我 运行 python 脚本很长一段时间内每 5 分钟一次,因此无法准确记录时间。 导入时间表 导入时间 从日期时间导入日期时间
# 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 但对我不起作用。 我在不同的 os
中使用 python 脚本你的 geeks 函数将花费时间来执行,并且计划作业在 geeks 完成后 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
因为schedule
does not account for the time it takes for the job function to execute。请改用 ischedule
。以下内容适用于您的任务。
import ischedule
ischedule.schedule(geeks, interval=2*60)
ischedule.run_loop()