在Python中以特定的5分钟间隔进行工作
我想通过:00,:05,:10,:15,:20,.....,:55。
因此,如果代码的执行启动 16:31:10 作业应在 16:35:00
正在使用以下代码:
import schedule
import time
from datetime import datetime, timezone, timedelta
print("Started Exec:- " + str(datetime.now()))
def job():
print("Job:- " + str(datetime.now()))
schedule.every(5).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
但是工作在 16:36:10
I would like to run the job at :00, :05 , :10, :15, :20, ....., :55.
So, if the execution of code started at 16:31:10 the job should run at 16:35:00
Am using the below code:
import schedule
import time
from datetime import datetime, timezone, timedelta
print("Started Exec:- " + str(datetime.now()))
def job():
print("Job:- " + str(datetime.now()))
schedule.every(5).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
But the job ran at 16:36:10
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用
。
I got this result using python-cron module:
In my case result looks like:
可以使用
Schedule
模块实现结果。首先计算时间到5分钟。运行一项从当时开始的工作,并使用该作业触发每5分钟运行一次的作业。
这大致给出了所需的结果,尽管时间似乎超过了5分钟的时间,几秒钟。
It is possible to achieve the result using the
schedule
module.First calculate the time until the 5 minute mark. Run a one off job that starts at that time, and use that job to trigger a job that runs every 5 minutes.
This gives roughly the desired result, although the time appears to drift beyond the 5 minute mark by a couple of seconds.