Django的芹菜任务总是阻塞

2024-09-29 17:23:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我的django设置中有以下设置:

CELERY_TASK_RESULT_EXPIRES = timedelta(minutes=30)
CELERY_CHORD_PROPAGATES = True
CELERY_ACCEPT_CONTENT = ['json', 'msgpack', 'yaml']
CELERY_ALWAYS_EAGER = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
BROKER_URL = 'django://'
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend'

我已在我已安装的应用程序下包含以下内容:

^{pr2}$

我的项目结构是(django 1.5)

proj
|_proj
  __init__.py
  celery.py      
  |_apps
    |_myapp1
      |_models.py
      |_tasks.py

这是我的celery.py文件:

from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings.dev')
app = Celery('proj')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS, related_name='tasks')

在主__init__.py中,我有:

from __future__ import absolute_import
from .celery import app as celery_app

最后在myapp1/任务.py我定义我的任务:

@task()
def retrieve():
  # Do my stuff

现在,如果我启动一个django交互式shell并启动retrieve任务:

result = retrieve.delay()

它似乎总是一个阻塞的调用,这意味着在函数返回之前提示被阻塞。result状态为SUCCESS,该函数实际执行操作,但似乎不是异步的。我错过了什么?在


Tags: djangofrompyimporttrueappsettingsresult
1条回答
网友
1楼 · 发布于 2024-09-29 17:23:20

似乎是CELERY_ALWAYS_EAGER造成的

if this is True, all tasks will be executed locally by blocking until the task returns. apply_async() and Task.delay() will return an EagerResult instance, which emulates the API and behavior of AsyncResult, except the result is already evaluated.

That is, tasks will be executed locally instead of being sent to the queue.

相关问题 更多 >

    热门问题