芹菜“收到未注册的任务”

时间:2016-05-14 11:00:37

标签: python celery

我是芹菜的新手,并试图在我的应用程序中使用它。以下是我的基本应用结构

 my_app
 |-run.py
 |-app
    |-mod1
    |-mod2
    |-tasks
       |-__init__.py
       |-email
       |-other_tasks_file

我想将所有后台任务限制在我的任务模块中。在我的 init .py任务中

from celery import Celery

celery = Celery('my_app', broker='redis://localhost:6379/0')

我的任务/电子邮件中有

from app.tasks import celery

@celery.task
def send_email():
    #do stuff

从终端我使用

启动工作人员
 celery -A app.tasks worker --loglevel=DEBUG

但我的任务没有出现在芹菜的任务列表中。此外,一旦我从解释器运行我的任务,如此

>>from app.tasks import email
>>email_result = email.send_email.delay()

当我这样做时,我在芹菜终端得到以下回应

Received unregistered task of type 'app.tasks.emails.send_email'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see url for more information.

The full contents of the message body was:
{'kwargs': {}, 'taskset': None, 'id': '51e8f766-e772-4d85-bad0-5a6774ea541a', 'eta': None, 'timelimit': (None, None), 'args': [], 'retries': 0, 'task': 'app.tasks.emails.send_email', 'utc': True, 'errbacks': None, 'chord': None, 'expires': None, 'callbacks': None} (283b)
Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/celery/app/utils.py", line 235, in find_app
sym = symbol_by_name(app, imp=imp)
File "/usr/local/lib/python3.4/site-packages/celery/bin/base.py", line 492, in symbol_by_name
return symbol_by_name(name, imp=imp)
File "/usr/local/lib/python3.4/site-packages/kombu/utils/__init__.py", line 101, in symbol_by_name
return getattr(module, cls_name) if cls_name else module
AttributeError: 'module' object has no attribute 'tasks'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/celery/worker/consumer.py", line 456, in on_task_received
strategies[name](message, body,
KeyError: 'app.tasks.channels.send_email'

我正在使用python 3.4和芹菜3.1.23

1 个答案:

答案 0 :(得分:17)

如果有人需要它,我终于开始工作了。 我需要做的是为一个包含任务的实际文件运行一个芹菜工人,以便芹菜注册任务 -

celery -A app.tasks.emails worker --loglevel=DEBUG  

因为只是运行

celery -A app.tasks worker --loglevel=DEBUG

(这是我疯狂的猜测)实际上不会导入我的send_email()任务。如果有人能给我一个解释,请做。