简体   繁体   English

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

[英]How can I schedule a Task to execute at a specific time using celery?

I've looked into PeriodicTask , but the examples only cover making it recur.我研究了PeriodicTask ,但示例仅涵盖使其重复发生。 I'm looking for something more like cron 's ability to say "execute this task every Monday at 1 am"我正在寻找更像是cron说“每周一凌晨 1 点执行此任务”的能力

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.在您的任务结束时,将其重新安排到下一次应该运行的时间。

The recently released version 1.0.3 supports this now, thanks to Patrick Altman!最近发布的 1.0.3 版现在支持这个,感谢 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 http://celeryproject.org/docs/changelog.html

I have just submitted a patch to add a ScheduledTask to accomplish a small bit of time based scheduling versus period based:我刚刚提交了一个补丁来添加一个 ScheduledTask 来完成一小部分基于时间的调度与基于周期的调度:

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

While @asksol's answer still holds, the api has been updated.虽然@asksol 的答案仍然成立,但 api 已更新。 For celery 4.1.0, I have to import crontab and periodic_task as follows:对于 celery 4.1.0,我必须按如下方式导入crontabperiodic_task

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

How you can read in this tutorial , you can make a PeriodicTask, i think if you have execute a task at 1 .am.如何阅读本教程,您可以创建一个 PeriodicTask,我想如果您在凌晨 1 点执行任务。 Monday's morning is because you wan to run a long cpu/mem operation, rememeber celery use ampq for enqueue tasks.星期一的早上是因为您要运行长时间的 cpu/mem 操作,请记住 celery 使用 ampq 进行排队任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM