Django芹菜击败任务不起作用

时间:2018-04-06 23:41:45

标签: python django celery django-celery celerybeat

celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TwitterApiProxy.settings')

app = Celery('TwitterApiProxy')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 10 seconds.
    sender.add_periodic_task(10.0, hello_test.s('hello'), name='add every 10')


@app.task
def hello_test(arg):
    print(arg)

settings.py

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'America/Los_Angeles'

我想打印"你好"每10秒钟。所以在我的终端中运行celery -A TwitterApiProxy beat时,我看到如下:

LocalTime -> 2018-04-06 23:27:09
Configuration ->
    . broker -> redis://localhost:6379//
    . loader -> celery.loaders.app.AppLoader
    . scheduler -> celery.beat.PersistentScheduler
    . db -> celerybeat-schedule
    . logfile -> [stderr]@%WARNING
    . maxinterval -> 5.00 minutes (300s)

它没有打印任何与我安排的任务相关的内容。我哪里做错了?

1 个答案:

答案 0 :(得分:2)

您的设置没有问题。

让你的工人和芹菜在两个单独的cmd窗户中击败

celery -A TwitterApiProxy worker -l info
celery -A TwitterApiProxy beat -l info

如果您使用的是Celery 4.0+,则必须先安装eventlet,然后按照以下方式启动您的工作人员:

celery -A TwitterApiProxy worker -l info -P eventlet

任务管理员后端

如果您想要任务管理员后端,可以安装并使用django-celery