python - celery "received unregistered task"

标签 python celery

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

但是我的任务没有出现在celery的任务列表中。另外,一旦我像这样从解释器运行我的任务

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

当我这样做时,我在我的 celery 终端中得到以下响应

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 和 celery 3.1.23

最佳答案

如果有人需要它,我终于让它工作了。 我需要做的是为包含任务的实际文件运行 celery worker,以便 celery 注册任务 -

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

因为只是运行

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

(这是我的大胆猜测)实际上不会导入我的 send_email() 任务。如果有人可以给我一个解释,请这样做。

关于python - celery "received unregistered task",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225803/

相关文章:

python - 如何向 exec 的运行代码发送值

python - neomodel 在保存时给出属性错误

python - 有没有更好的方法来评估用户输入而无需 'for' 循环并将检查器从 False 更改为 True?

django - 分离 celery 用于开发和生产

python - 在 python Flask 应用程序中异步发送欢迎电子邮件

没有 amqp 服务器的 django-celery (rabbitmq)

django - 是否有人将 django celery worker 实现为 docker 容器,它仅在分配任务时运行

python - 我正在尝试网络抓取,但得到的是函数而不是实际数据

python - Python 3 中的 writerow 总是在原始行之间写一个空行,Python2 工作得很好。为什么?

docker - 在 Docker 容器内运行 Celery 任务时出现问题