Django 1.9 + Celery未注册的任务

时间:2016-07-23 15:41:33

标签: python django celery django-celery

  

此配置正确。我正在吃芹菜   错误的方式:(,没有指定项目名称。(芹菜工人-A   hockey_manager -l info

我确实从1.6.5升级到Django 1.9,我无法再次使芹菜配置工作。

经过近两天寻找解决方案后,我找不到任何工作。

芹菜没有检测到我的任务。我尝试过:

  • CELERY_IMPORTS
  • autodiscover_tasks
  • 绑定=真

依赖关系

amqp==2.0.3 
celery==3.1.23 
Django==1.9.8 
django-celery==3.1.17
kombu==3.0.35

项目结构

enter image description here

hockey_manager / __初始化__。PY

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

hockey_manager / celery.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', 'hockey_manager.settings.common')

app = Celery('hockey_manager')

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

# load task modules from all registered Django app configs.
app.autodiscover_tasks(['hockey'])

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

# Celery backend configs
app.conf.update(
    CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend',
)

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


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

hockey_manager /设置/ common.py

INSTALLED_APPS = [
    ...
    'hockey',
    'djcelery',
    'kombu.transport.django',
    ...
]

##
# Celery
##
# BROKER_POOL_LIMIT = 3
BROKER_URL = os.environ.get('CLOUDAMQP_URL')
#BROKER_URL = 'django://'

# List of modules to import when celery starts.
CELERY_IMPORTS = ('hockey.tasks', )

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)

CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

曲棍球/ tasks.py

from __future__ import absolute_import
from hockey_manager.celery import app

@app.task(name='hockey.tasks.league_schedule_results')
def league_schedule_results(schedule, statistics):
    ...
来自Celery的

错误

  

[2016-07-23 17:05:46,231:ERROR / MainProcess]收到未注册   'hockey.tasks.league_schedule_results'类型的任务。       该消息已被忽略并被丢弃。

我还从版本2.0开始从 amqp 收到弃用警告:

  

AMQPDeprecationWarning:连接上的.transport属性是   之前访问过       建立了联系。这是现在支持,但会       在amqp 2.2.0中弃用。

Since amqp 2.0 you have to explicitly call Connection.connect()
before using the connection.

  W_FORCE_CONNECT.format(attr=attr)))

1 个答案:

答案 0 :(得分:6)

使用django-celery模块。

这里有link示例如何将django-celery与django 1.9.1 +

一起使用