简体   繁体   中英

PeriodicTask with Django + Celery

I am trying to do a Periodic task in my project but I can't make it work. In my dev I execute celery like this:

python manage.py celery worker -l info

But when I try:

celery -B -l info

or

python manage.py celery -B -l info

I get a Connection refused error . I have also installed flower to monitoring if the task is executed. I have other tasks executen from code, but now I'd like to do a periodical task.

My tasks (that actually works) inherits from celery.task.Task and the Task I want to be periodical from celery.task.PeriodicTask , something like this:

from celery.task import PeriodicTask

TaskToBePeriodical(PeriodicTask):
    run_every = crontab()  # This should make it execute every minute, just for test
    def run(self, **kwargs):
        # Do something awesome...

It looks like your command to start celery isn't quite correct.
Depending on what you are trying to schedule, you may want to try setting it up as as celery.beat.

It might be worth noting that this method does not use django-celery, as the Celery docs mentions that, "THIS PROJECT IS NO LONGER REQUIRED"

See here to learn more about celery.beat: http://celery.readthedocs.org/en/latest/reference/celery.beat.html

Celery start command

celery -A proj.celery_app worker -l info -B

Project Directories

+-- proj
|   +-- celery_app.py
|   +-- settings.py
|   +-- urls.py
|   +-- ...
+-- app
|   +-- models.py
|   +-- tasks.py
|   +-- urls.py
|   +-- views.py
|   +-- ...

celery_app.py

from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

from django.conf import settings

app = Celery('proj')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

settings.py

CELERYBEAT_SCHEDULE = {
    'name_of_task': {
        'task': 'app.tasks.periodic_task',
        'schedule': crontab()
    }
}

tasks.py

from __future__ import absolute_import
from celery import shared_task

@shared_task
def periodic_task():
    # Do your awesome thing
    return

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