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

24,464

Solution 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

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

Solution 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

Solution 4

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
Share:
24,464
Hank Gay
Author by

Hank Gay

I like to spend time with my lovely wife, our two beautiful daughters, and our dog. I'm also a fan of geek humor, and I've been known to flip out and write code… elegant code, if I'm really lucky.

Updated on July 09, 2022

Comments

  • Hank Gay
    Hank Gay almost 2 years

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