简体   繁体   中英

how can I run celery task only once in django?

from __future__ import absolute_import, unicode_literals

from celery import shared_task
from celery.task import periodic_task
from celery.schedules import crontab
from datetime import timedelta



@periodic_task(run_every=(crontab(minute='*/1')), name='greeting_task')
def greeting_task():
    print('hello Dias!')

Can I create a function that runs only once at certain time with crontab? PLEASE, HELP!!! thanks in advance!

If you are using Django-Celery-Beat , it has the ability to create tasks that run only once at a specific date/time using the ClockedSchedule model. It's not in the documentation for some reason but you can easily configure it through the Django Admin.

You need to change the parameters for crontab .

Example: If you want the task to be run once at 5AM everyday:

@periodic_task(run_every=(crontab(minute='0', hour='5')), name='greeting_task')
def greeting_task():
    print('hello Dias!')

crontab(minute='*/1') will run the task at every minute. Read about crontab syntax here: https://en.wikipedia.org/wiki/Cron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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