Celery在Django 2.1.1中找不到任务或设置

时间:2018-10-20 11:20:52

标签: python django rabbitmq celery django-2.1

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

找不到任务,它以默认设置开始,而忽略了设置文件中提到的用户名,密码和虚拟主机。 Tasks are not found and it starts with default settings ignoring my username and password and vhost mentioned in settings file.

项目目录:

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

2 个答案:

答案 0 :(得分:2)

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

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

    app.autodiscover_tasks() 
    

    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    

答案 1 :(得分:0)

实际问题出在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