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

China☆狼群 提交于 2019-12-17 08:51:27

问题


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."


回答1:


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




回答2:


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.




回答3:


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




回答4:


How you can read in this tutorial, you can make a PeriodicTask, i think if you have execute a task at 1 .am. Monday's morning is because you wan to run a long cpu/mem operation, rememeber celery use ampq for enqueue tasks.




回答5:


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


来源:https://stackoverflow.com/questions/1990531/how-can-i-schedule-a-task-to-execute-at-a-specific-time-using-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!