【问题标题】:Celery can not find tasks nor settings with Django 2.1.1Celery 无法使用 Django 2.1.1 找到任务或设置
【发布时间】:2018-10-20 11:20:52
【问题描述】:

我正在尝试使用 Celery 和 RabbitMQ 服务器执行异步任务。我在我的系统上安装了CeleryRabbitMQ。现在,当我运行celery worker -l info 时,芹菜开始使用默认配置设置工作,忽略我的设置,并且它显示没有注册任务。我假设由于我的项目结构而出现问题。但是现在改不了了。谁能帮我弄清楚这里有什么问题?

找不到任务,它以默认设置开始,忽略我的用户名和密码以及设置文件中提到的虚拟主机。

项目目录:

|--engine
|  |--app
|  |   |--user
|  |   |--program
|  |   |  |--__init__.py
|  |   |  |--admin.py
|  |   |  |--apps.py
|  |   |  |--models.py
|  |   |  |--tasks.py
|  |   |  |--urls.py
|  |   |  |--views.py
|  |   |--course
|  |--config
|  |   |--settings
|  |   |  |--__init.py
|  |   |  |--default.py
|  |   |  |--development.py
|  |   |  |--production.py
|  |   |--__init__.py
|  |   |--celery.py
|  |   |--middleware.py
|  |   |--urls.py
|  |   |--wsgi.py
|  |--.env
|  |--manage.py
|  |--requirements.txt

engine/config/celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.default')

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


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

engine/config/__init__.py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app

__all__ = ('celery_app',)

engine/app/program/tasks.py

from celery import shared_task


@shared_task()
def add(number1, number2):
    print(number1 + number2)

engine/config/settings/default.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'corsheaders',
    'django_filters',

    'app.program',
    'app.course',
    'app.user',
]

CELERY_BROKER_URL = 'amqp://uname:pass@localhost:5672/vhost/'

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

【问题讨论】:

  • 你在哪里调用 add.delay(..) 函数?
  • 这在我的 app/programs/views.py 文件中。它实际上更多的是配置问题。因为当我运行 celery worker 时,我的任务应该在 [tasks] 中可用,但未显示。

标签: python django rabbitmq celery django-2.1


【解决方案1】:

到目前为止,我发现您的配置存在三个问题:

  1. 在设置文件中,您应该按名称 BROKER_URL 而不是 CELERY_BROKER_URL 指定代理 URL。
  2. tasks.py 中指定的装饰器应该是@shared_task 而不是@shared_task()
  3. 指定 celery 寻找任务的路径。在 celery.py 文件中,更新

    app.autodiscover_tasks() 
    

    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    

【讨论】:

  • 我应该从哪里导入设置?是from django.conf import settings 吗?
  • 根据文档namespace="CELERY" means all celery-related configuration keys should have a "CELERY_" prefix你能定义一下吗?我已经完成了你的设置,结果还是一样。
【解决方案2】:

实际问题出在 Windows 上。正如他们网站上提到的 -

Does Celery support Windows?
Answer: No.

Since Celery 4.x, Windows is no longer supported due to lack of resources.

But it may still work and we are happy to accept patches.

资源:http://docs.celeryproject.org/en/latest/faq.html#windows

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 2021-11-14
    • 2011-06-19
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多