python - Celery 4 不自动发现任务

标签 python django celery

我有一个 Django 1.11 和 Celery 4.1 项目,我已经根据 setup docs 配置了它。 .我的 celery_init.py 看起来像

from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings.settings'

app = Celery('myproject')

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

#app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) # does nothing
app.autodiscover_tasks() # also does nothing

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

但是,当我启动一个 worker 时:

.env/bin/celery worker -A myproject -l info

它显示除了示例“debug_task”之外没有找到任何任务,即使我安装了几个带有 Celery 任务的应用程序,应该通过调用 app.autodiscover_task() 找到。这是我的工作人员生成的初始输出:

 -------------- celery@localhost v4.1.0 (latentcall)
---- **** ----- 
--- * ***  * -- Linux-4.13.0-16-generic-x86_64-with-Ubuntu-16.04-xenial 2017-10-31 15:56:42
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         myproject:0x7f952856d650
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     amqp://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . myproject.celery_init.debug_task

[2017-10-31 15:56:42,180: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2017-10-31 15:56:42,188: INFO/MainProcess] mingle: searching for neighbors
[2017-10-31 15:56:43,211: INFO/MainProcess] mingle: all alone
[2017-10-31 15:56:43,237: INFO/MainProcess] celery@localhost ready.

我的应用 tasks.py 文件中的所有遗留任务定义如下:

from celery.task import task

@task(name='mytask')
def mytask():
    blah

文档建议使用 shared_task 装饰器,所以我尝试了:

from celery import shared_task

@shared_task
def mytask():
    blah

但是我的 Celery worker 仍然没有看到它。我做错了什么?

编辑:我已经能够通过在我的设置的 CELERY_IMPORTS 列表中明确列出它们来让任务显示出来,但即使那样我也必须大量编辑 tasks.py 删除我的 Django 项目(models.py 等)的所有导入,否则它会引发异常 Apps aren't loaded yet. 这总比没有好,但需要大量重构。有没有更好的办法?

最佳答案

我遇到了类似的问题,解决方案是将 include kwarg 添加到您的 celery 调用中。

The include argument is a list of modules to import when the worker starts. You need to add our tasks module here so that the worker is able to find our tasks.

app = Celery('myproject', 
             backend = settings.CELERY.get('backend'),
             broker = settings.CELERY.get('broker'),
             include = ['ingest.tasks.web', ... ])

查看 http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#proj-celery-py了解更多信息

关于python - Celery 4 不自动发现任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47043693/

相关文章:

python - 如何使用 Pathlib 在 Python 中迭代目录

python - 有没有理由在 multiprocessing.Lock 上使用 threading.Lock?

python - 基于多个输入(如月份和固定值列)预测多个输出

html - Django - 模板表单复选框

python - Django 选择表单字段的标签

python - 使用 Celery 初始化带有参数的 worker

python - 在 python 单元测试中断言 `is`

python - 测试经过身份验证的 django-rest-framework 路由时出现 405 错误

python - 我可以将 Python 请求与 celery 一起使用吗?

python-2.7 - 不通过使用 RabbitMQ 运行 celery 的 Airflow 执行的作业