我使用下面的代码每5分钟执行一次python脚本,但是当它下次执行时,它不会像以前那样在excute时间执行。例如,如果我在上午9点整执行,下次执行时间是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,因为我已经尝试了crontab不为我工作。我在不同的操作系统中使用python脚本。
发布于 2020-11-23 05:42:35
您的极客函数将花费时间来执行,并且计划作业开始计算在极客完成后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 发布于 2021-08-19 19:04:41
是因为schedule 不考虑执行作业函数所需的时间。。使用ischedule代替。以下内容将适用于您的任务。
import ischedule
ischedule.schedule(geeks, interval=2*60)
ischedule.run_loop()https://stackoverflow.com/questions/64963033
复制相似问题