Django,Celery,Redis:AttributeError:模块'imp_expert'没有属性'celery'

时间:2019-01-24 14:50:58

标签: python django redis celery config

根据Celery文档,尝试启动celery -A imp_expert worker -l info(我的项目未命名为proj,而是imp_expert

我得到:

Traceback (most recent call last):
  File "/home/expert/.virtualenvs/semion_expert/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/__main__.py", line 16, in main
    _main()
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/bin/celery.py", line 322, in main
    cmd.execute_from_commandline(argv)
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/bin/celery.py", line 496, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/bin/base.py", line 273, in execute_from_commandline
    argv = self.setup_app_from_commandline(argv)
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/bin/base.py", line 479, in setup_app_from_commandline
    self.app = self.find_app(app)
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/bin/base.py", line 501, in find_app
    return find_app(app, symbol_by_name=self.symbol_by_name)
  File "/home/expert/.virtualenvs/semion_expert/lib/python3.5/site-packages/celery/app/utils.py", line 370, in find_app
    found = sym.celery
AttributeError: module 'imp_expert' has no attribute 'celery'

我在__init__.py中有

from __future__ import absolute_import
from .celery import app as celery_app

__all__ = ('celery_app',)

celery.py中:

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

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'imp_expert.config.base')

app = Celery('imp_expert')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


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

base.py中(我们有base.pylocal.pyproduction.py设置文件):

from __future__ import absolute_import, unicode_literals # At the top

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

BROKER_URL = 'redis://127.0.0.1:6379'

CELERY_BROKER_URL = env('CELERY_BROKER_URL', default='redis://127.0.0.1:6379')
if CELERY_BROKER_URL == 'django://':
    CELERY_RESULT_BACKEND = 'redis://'
else:
    CELERY_RESULT_BACKEND = CELERY_BROKER_URL

文件夹和项目结构为:

sem_expert
├──config
|  ├──settings
|  |  ├──__init__.py
|  |  ├──base.py
|  |  ├──local.py
|  |  └──production.py
|  ├──__init__.py
|  ├──celery.py
|  ├──urls.py
|  └──wsgi.py
├──imp_expert
|  └──expert
|     └──api
|        └──tasks.py

(我完全知道,在其中包含名为imp_expert的项目和其中包含名为expert的应用程序会令人困惑,但这是在我很久以前完成的,而且还会做得更多。麻烦,而不是现在就值得更改)

我想我不清楚如何使用celery.py来引用配置,特别是在网上: os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'imp_expert.config.base') 要么,要么我对应该指向celery的位置感到困惑。

在此问题上提前感谢您的帮助。在项目的这个阶段,我还是新手。

Django 1.10.8 芹菜4.2.1 Redis 2.10.6

0 个答案:

没有答案