Periodic Tasks with Celery and Django

人走茶凉 提交于 2019-12-22 08:50:30

问题


I'm having troubles getting a periodic tasks to run with Celery 3.1.8, Django 1.6.1, and RabbitMQ. I'm a bit confused with the current documentation as I understand that django-celery is not needed anymore to get Celery running with Django. I have a feeling that I'm not running the worker correctly, but after searching for a solution on SO and googling, I'm in need of help. Could anyone point me in the right direction with this?

settings.py (not sure if I need this since I have a @periodic_task decorator on my task)

CELERYBEAT_SCHEDULE = {
    'add-every-30-seconds': {
        'task': 'tasks.send_test_email',
        'schedule': datetime.timedelta(seconds=30)
    },
}

My app (celery.py)

from __future__ import absolute_import

import os
from celery import Celery
from django.conf import settings

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

app = Celery('app',
             broker='amqp://',
             backend='amqp://',
             include=['app.tasks'])


app.conf.update(
    CELERY_TASK_RESULT_EXPIRES=3600,
    CELERY_TIMEZONE='Europe/Oslo',
    )

if __name__ == '__main__':
    app.start()

# 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))

Periodic task (tasks.py)

from __future__ import absolute_import
from celery.task import periodic_task
import datetime

@periodic_task(run_every=datetime.timedelta(minutes=1))
def send_test_email():
    print "This is a periodic task from celery"

On the command line, I'm executing the worker:

celery worker -A app -l info
celery beat

回答1:


Some other solution would be to use the @periodic_task celery decorator

from celery.schedules import crontab

@periodic_task(run_every=crontab(minute=0, hour=1))
def my_task():
    print 'my_task'


来源:https://stackoverflow.com/questions/21464995/periodic-tasks-with-celery-and-django

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