简体   繁体   English

Celery和Django的定期任务

[英]Periodic Tasks with Celery and Django

I'm having troubles getting a periodic tasks to run with Celery 3.1.8, Django 1.6.1, and RabbitMQ. 我在使用Celery 3.1.8,Django 1.6.1和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. 我对当前的文档有点困惑,因为据我所知,不再需要django-celery来让Celery与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. 我有一种感觉,我没有正确地运行工作人员,但在寻找SO和google搜索解决方案之后,我需要帮助。 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) settings.py(不确定我是否需要这个,因为我的任务上有@periodic_task装饰器)

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

My app (celery.py) 我的应用程序(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) 定期任务(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: 在命令行上,我正在执行worker:

celery worker -A app -l info
celery beat

Some other solution would be to use the @periodic_task celery decorator 其他一些解决方案是使用@periodic_task 芹菜装饰器

from celery.schedules import crontab

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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