我如何在特定时间后每隔 30 分钟安排一次 Python 代码

发布于 2025-01-16 12:49:52 字数 409 浏览 0 评论 0原文

我有一个时间 - “09:20:45” 我想在这个时间的每 30 分钟之后安排我的代码 - “09:20:45”。

我不知道我该怎么做?

schedule.every(30).minutes.do(output_file_exporting)
while True:
    schedule.run_pending()
    time.sleep(1)

其中 output_file_exporting 是我的函数名称。

所以我想在 09:20:45 之后以每 30 分钟的增量运行我的代码 就像-

09:50:45
10:20:45
10:50:45
11:20:45
11:50:45

在这些时候我的代码应该运行。

I have a time - "09:20:45"
And I want to schedule my code after every 30 minutes of this time - "09:20:45".

I don't know how can I do this?

schedule.every(30).minutes.do(output_file_exporting)
while True:
    schedule.run_pending()
    time.sleep(1)

where output_file_exporting is my function name.

So I want to run my code after 09:20:45 on the increment of every 30 minutes
like -

09:50:45
10:20:45
10:50:45
11:20:45
11:50:45

on these time my code should be run.

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

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

发布评论

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

评论(1

在风中等你 2025-01-23 12:49:53

你让事情变得比需要的更加困难。你只要按照你描述的那样去做就可以了。

import time
import datetime

while True:
    now = datetime.datetime.now()
    if now.hour >= 9 and now.minute in (20,50):
        break
    time.sleep(60)

output_file_exporting()
schedule.every(30).minutes.do(output_file_exporting)
...

You're making this harder than it needs to be. You just do it like you describe it.

import time
import datetime

while True:
    now = datetime.datetime.now()
    if now.hour >= 9 and now.minute in (20,50):
        break
    time.sleep(60)

output_file_exporting()
schedule.every(30).minutes.do(output_file_exporting)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文