1

我正在尝试使用 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

引擎/配置/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))

引擎/配置/__init__.py

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

__all__ = ('celery_app',)

引擎/应用程序/程序/tasks.py

from celery import shared_task


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

引擎/配置/设置/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
4

2 回答 2

2

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

  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)
    
于 2018-10-20T15:03:34.673 回答
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

于 2018-10-23T06:04:06.140 回答