18

我已经研究过PeriodicTask,但这些示例仅涵盖使其重复发生。我正在寻找更像cron是说“每周一凌晨 1 点执行此任务”的能力

4

5 回答 5

37

采用

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

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

于 2010-01-02T04:24:01.583 回答
32

最近发布的 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

于 2010-01-22T08:54:04.853 回答
6

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

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

于 2010-05-13T20:54:49.117 回答
1

虽然@asksol 的回答仍然成立,但 api 已更新。crontab对于 celery 4.1.0 ,我必须导入periodic_task如下:

from celery.schedules import crontab
from celery.task import periodic_task
于 2017-11-09T21:57:00.450 回答
0

你如何阅读本教程,你可以制作一个 PeriodicTask,我想如果你在凌晨 1 点执行了一个任务。星期一的早上是因为你想运行一个很长的 cpu/mem 操作,记住 celery 使用 ampq 进行入队任务。

于 2010-01-02T04:41:43.567 回答