为什么django.setup()会引发错误?

时间:2018-06-03 19:31:01

标签: python django celery

我的 celery.py 文件包含下一个代码。当我尝试执行此命令时,此代码引发错误:python manage.py runserver。但同时celery任务使用此命令:celery -A TestProject worker -l info -B

我用:芹菜== 4.1.1 Django == 1.11.5 Redis == 3.0.6

celery.py

from __future__ import absolute_import
import os
import sys
from celery import Celery
from celery._state import _set_current_app
import django
from django.conf import settings


app = Celery('TestProject')

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

_set_current_app(app)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestProject.settings')

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../EnjoyJumping')))

django.setup()

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

错误:

AttributeError: 'Settings' object has no attribute 'ACCOUNT_PASSWORD_STRIP'

如果从django.setup()文件中删除celery.py,则runserver命令有效,但celery命令不起作用。它引发了错误:

[2018-06-04 01:17:21,407: ERROR/MainProcess] Received unregistered task of type 'custom_app.tasks.counting'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.

The full contents of the message body was:
b'[[], {}, {"chain": null, "chord": null, "callbacks": null, "errbacks": null}]' (77b)
Traceback (most recent call last):
  File "/home/ubuntu/enjoy_jumping/lib/python3.5/site-packages/celery/worker/consumer/consumer.py", line 557, in on_task_received
    strategy = strategies[type_]
KeyError: 'custom_app.tasks.counting'

我真的很困惑。如何解决这个问题?

0 个答案:

没有答案