我使用下面的代码每 5 分钟执行一次 python 脚本,但是当它下次执行时,它不会像以前那样在精确的时间执行。
例如,如果我在上午 9:00:00 准确执行它,则下次它在上午 9:05:25 执行,下次在上午 9:10:45 执行。由于我每 5 分钟运行一次 python 脚本很长一段时间,它无法准确记录时间。
进口时间表
导入时间
从日期时间导入日期时间
# 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,因为我已经尝试过 crontabs 但不适合我。
我在不同的操作系统中使用 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)