如何使用 celery 安排任务在特定时间执行?

发布于 2024-08-16 18:27:23 字数 99 浏览 1 评论 0原文

我研究过PeriodicTask,但示例仅涵盖使其重复出现。我正在寻找更像 cron 的能力,可以说“每周一凌晨 1 点执行此任务”

I've looked into PeriodicTask, but the examples only cover making it recur. I'm looking for something more like cron's ability to say "execute this task every Monday at 1 a.m."

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

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

发布评论

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

评论(4

神经大条 2024-08-23 18:27:23

使用

YourTask.apply_async(args=[some, args, here], eta=when)

并在任务结束时,将其重新安排到下一次应该运行的时间。

Use

YourTask.apply_async(args=[some, args, here], eta=when)

And at the end of your task, reschedule it to the next time it should run.

贱人配狗天长地久 2024-08-23 18:27:23

最近发布的 1.0.3 版本现在支持这一点,感谢 Patrick Altman!

示例:

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=crontab(hour=7, minute=30, day_of_week="mon"))
def every_monday_morning():
    print("This runs every Monday morning at 7:30a.m.")

有关详细信息,请参阅变更日志:

http://celeryproject.org/docs/changelog.html

The recently released version 1.0.3 supports this now, thanks to Patrick Altman!

Example:

from celery.task.schedules import crontab
from celery.decorators import periodic_task

@periodic_task(run_every=crontab(hour=7, minute=30, day_of_week="mon"))
def every_monday_morning():
    print("This runs every Monday morning at 7:30a.m.")

See the changelog for more information:

http://celeryproject.org/docs/changelog.html

万劫不复 2024-08-23 18:27:23

我刚刚提交了一个补丁来添加 ScheduledTask 来完成一小部分基于时间的调度与基于周期的调度:

https://github.com/celery/celery/commit/e8835f1052bb45a73f9404005c666f2d2b9a9228

I have just submitted a patch to add a ScheduledTask to accomplish a small bit of time based scheduling versus period based:

https://github.com/celery/celery/commit/e8835f1052bb45a73f9404005c666f2d2b9a9228

蓝眼泪 2024-08-23 18:27:23

虽然 @asksol 的答案仍然有效,但 api 已更新。对于 celery 4.1.0,我必须导入 crontab 和 periodic_task ,如下所示:

from celery.schedules import crontab
from celery.task import periodic_task

While @asksol's answer still holds, the api has been updated. For celery 4.1.0, I have to import crontab and periodic_task as follows:

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