在Python中以特定的5分钟间隔进行工作

发布于 2025-01-23 14:03:32 字数 662 浏览 3 评论 0原文

我想通过: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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笛声青案梦长安 2025-01-30 14:03:32

我使用

from datetime import datetime
import pycron


@pycron.cron('*/5 * * * * 0')
async def test(dt: datetime):
    print(f"test cron job running at {dt}")

if __name__ == '__main__':
    print(f"Started at {datetime.now().strftime('%Y-%m-%d, %H:%M:%S')}")
    pycron.start()

Started at 2022-09-08, 11:03:54
test cron job running at 2022-09-08 11:05:00.059370
test cron job running at 2022-09-08 11:10:00.891355
test cron job running at 2022-09-08 11:15:00.700347

I got this result using python-cron module:

from datetime import datetime
import pycron


@pycron.cron('*/5 * * * * 0')
async def test(dt: datetime):
    print(f"test cron job running at {dt}")

if __name__ == '__main__':
    print(f"Started at {datetime.now().strftime('%Y-%m-%d, %H:%M:%S')}")
    pycron.start()

In my case result looks like:

Started at 2022-09-08, 11:03:54
test cron job running at 2022-09-08 11:05:00.059370
test cron job running at 2022-09-08 11:10:00.891355
test cron job running at 2022-09-08 11:15:00.700347
提笔落墨 2025-01-30 14:03:32

可以使用Schedule模块实现结果。

首先计算时间到5分钟。运行一项从当时开始的工作,并使用该作业触发每5分钟运行一次的作业。

import schedule
import time
from datetime import datetime, timedelta


print("Started Exec:- " + str(datetime.now()))

def repeat_job():
    print("repeat_job:- " + str(datetime.now()))

def initial_job():
    print("initial_job:- " + str(datetime.now()))
    schedule.every(5).minutes.do(repeat_job)
    return schedule.CancelJob

# Round the current time to the next 5 minute mark.
tm = datetime.now()
tm -= timedelta(minutes=tm.minute % 5,
                             seconds=tm.second,
                             microseconds=tm.microsecond)
tm += timedelta(minutes=5)

schedule.every().day.at(tm.strftime("%H:%M")).do(initial_job)

while True:
    schedule.run_pending()
    time.sleep(1)

这大致给出了所需的结果,尽管时间似乎超过了5分钟的时间,几秒钟。

Started Exec:- 2022-09-08 16:13:13.010702
initial_job:- 2022-09-08 16:15:00.181704
repeat_job:- 2022-09-08 16:20:00.638851
repeat_job:- 2022-09-08 16:25:01.066414
repeat_job:- 2022-09-08 16:30:01.492325
repeat_job:- 2022-09-08 16:35:01.899328
repeat_job:- 2022-09-08 16:40:02.353182
repeat_job:- 2022-09-08 16:45:02.785273

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.

import schedule
import time
from datetime import datetime, timedelta


print("Started Exec:- " + str(datetime.now()))

def repeat_job():
    print("repeat_job:- " + str(datetime.now()))

def initial_job():
    print("initial_job:- " + str(datetime.now()))
    schedule.every(5).minutes.do(repeat_job)
    return schedule.CancelJob

# Round the current time to the next 5 minute mark.
tm = datetime.now()
tm -= timedelta(minutes=tm.minute % 5,
                             seconds=tm.second,
                             microseconds=tm.microsecond)
tm += timedelta(minutes=5)

schedule.every().day.at(tm.strftime("%H:%M")).do(initial_job)

while True:
    schedule.run_pending()
    time.sleep(1)

This gives roughly the desired result, although the time appears to drift beyond the 5 minute mark by a couple of seconds.

Started Exec:- 2022-09-08 16:13:13.010702
initial_job:- 2022-09-08 16:15:00.181704
repeat_job:- 2022-09-08 16:20:00.638851
repeat_job:- 2022-09-08 16:25:01.066414
repeat_job:- 2022-09-08 16:30:01.492325
repeat_job:- 2022-09-08 16:35:01.899328
repeat_job:- 2022-09-08 16:40:02.353182
repeat_job:- 2022-09-08 16:45:02.785273
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文