gpt4 book ai didi

python - Django 1.9 + Celery 未注册任务

转载 作者:太空宇宙 更新时间:2023-11-04 08:02:01 26 4
gpt4 key购买 nike

This configuration is correct. I was starting celery the wrong way :(, without specifying the project name. (celery worker -A hockey_manager -l info

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

在寻找解决方案将近两天后,我没有找到任何有效的方法。

Celery 没有检测到我的任务。我试过:

  • CELERY_IMPORTS
  • autodiscover_tasks
  • 绑定(bind)=真

依赖关系

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/__init__.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/settings/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] Received unregistered task of type 'hockey.tasks.league_schedule_results'. The message has been ignored and discarded.

从 2.0 版开始,我还收到来自 amqp 的弃用警告:

AMQPDeprecationWarning: The .transport attribute on the connection was accessed before the connection was established. This is supported for now, but will be deprecated in 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)))

最佳答案

使用 django-celery 模块。

这里是 link举例说明如何在 django 1.9.1+ 中使用 django-celery

关于python - Django 1.9 + Celery 未注册任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38543513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com