Celery任务组未在后台执行,并导致异常 [英] Celery task group not being executed in background and results in exception
问题描述
我的Celery任务未在我的Django 1.7/Python3项目中的后台执行.
My Celery task isn't executing in the background in my Django 1.7/Python3 project.
# settings.py
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULTBACKEND = BROKER_URL
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERY_ALWAYS_EAGER = False
我的根应用模块中有 celery.py
,例如:
I have celery.py
in my root app module as such:
from __future__ import absolute_import
import os
import django
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_app.settings')
django.setup()
app = Celery('my_app')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
并在根模块的 __ init __.py
中加载应用程序:
and load the app in __init__.py
in the root module:
from __future__ import absolute_import
from .celery import app as celery_app
我的任务在我的应用程序模块中的task.py文件中设置为共享任务:
My task is set up as a shared task in a tasks.py file in my app module:
from __future__ import absolute_import
from celery import shared_task
@shared_task
def update_statistics(profile, category):
# more code
然后我将该任务称为一个小组:
and I call the task as a group:
. . .
job = group([update_statistics(f.profile, category)
for f in forecasts])
job.apply_async()
但是,我在任务队列中没有看到任何状态更新,而我正在通过以下方式启动
However, I'm not seeing any status updates in my task queue, which I am starting via:
$ celery -A my_app worker -l info
正在执行任务 ,只是不在后台执行.如果在任务代码中添加一条打印语句,我将在Django开发服务器控制台中看到输出,而不是Celery队列.
The task is being executed, just not in the background. If I add a print statement to the task code, I will see the output in my Django development server console instead of the Celery queue.
任务在前台运行后,我遇到了以下异常:
After the task runs in the foreground, I'm greeted with this exception:
'NoneType' object has no attribute 'app'
如果您有兴趣,这里是完整的追溯: https://gist.github.com/alsoicode/0263d251e3744227ba46
Here's the full traceback if you're interested: https://gist.github.com/alsoicode/0263d251e3744227ba46
推荐答案
创建组时,您直接在列表理解中调用任务,因此它们会在那里执行.您需要使用 .subtask()
方法(或其快捷方式 .s()
)来创建子任务而不调用它们:
You're calling the tasks directly in your list comprehension when you create the group, so they're executed then and there. You need to use the .subtask()
method (or its shortcut, .s()
) to create the subtasks without calling them:
job = group([update_statistics.s(f.profile, category) for f in forecasts])
这篇关于Celery任务组未在后台执行,并导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!