Celery 4不自动发现任务 [英] Celery 4 not auto-discovering tasks

查看:273
本文介绍了Celery 4不自动发现任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django 1.11和Celery 4.1项目,并且已经根据设置文档。我的 celery_init.py 看起来是

I have a Django 1.11 and Celery 4.1 project, and I've configured it according to the setup docs. My celery_init.py looks like

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))

但是,当我启动一个具有以下条件的工人时:

However, when I launch a worker with:

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

它显示除示例 debug_task外未找到任何任务,即使我已经安装了一些带有Celery任务的应用程序,也应该通过调用来找到 app.autodiscover_task()。这是我的工作人员生成的初始输出:

it shows no tasks being found except for the sample "debug_task", even though I have several installed apps with Celery tasks, with should have been found via the call to app.autodiscover_task(). This is the initial output my worker generates:

 -------------- 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 文件的定义如下:

All my legacy tasks in my app tasks.py files were defined like:

from celery.task import task

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

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

The docs suggest using the shared_task decorator, so instead I tried:

from celery import shared_task

@shared_task
def mytask():
    blah

但是我的芹菜工人仍然看不到它。我在做什么错了?

But my Celery worker still doesn't see it. What am I doing wrong?

编辑:通过在设置的 CELERY_IMPORTS中明确列出这些任务,我已经能够显示任务。 code>列表,但是即使如此,我仍然必须大量编辑 tasks.py 来删除我的Django项目的所有导入(models.py等),否则它会引发例外情况应用程序尚未加载。这总比没有好,但是需要大量的重构。有更好的方法吗?

I've been able to get tasks to show up by explicitly listing them in my setting's CELERY_IMPORTS list, but even then I have to heavily edit the tasks.py to remove all imports of my Django project (models.py, etc) or it raises the exception Apps aren't loaded yet. This is better than nothing, but requires a huge amount of refactoring. Is there a better way?

推荐答案

我发现更大的问题是Celery没有设置我的自定义 Celery()实例作为当前应用。要解决此问题,我必须修改 celery_init.py 使其包括:

I found the bigger problem was that Celery wasn't setting my custom Celery() instance as the current app. To fix it, I had to modify my celery_init.py to include:

from celery._state import _set_current_app

# setup my app = Celery(...)

_set_current_app(app)

这篇关于Celery 4不自动发现任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆