【问题标题】:How to get the "full" async result in Celery link_error callback如何在 Celery link_error 回调中获取“完整”异步结果
【发布时间】:2015-11-06 11:42:54
【问题描述】:

我有 Celery 3.1.18 与 Django 1.6.11 和 RabbitMQ 3.5.4 一起运行,并尝试在失败状态下测试我的异步任务 (CELERY_ALWAYS_EAGER=True)。但是,我无法在错误回调中获得正确的“结果”。 Celery docs 中的示例显示:

@app.task(bind=True)
def error_handler(self, uuid):
    result = self.app.AsyncResult(uuid)
    print('Task {0} raised exception: {1!r}\n{2!r}'.format(
          uuid, result.result, result.traceback))

当我这样做时,我的结果仍然是“PENDING”、result.result = ''result.traceback=''。但是我的.apply_async 调用返回的实际结果具有正确的“FAILURE”状态和回溯。

我的代码(基本上是一个 Django Rest Framework RESTful 端点,它解析 .tar.gz 文件,然后在文件解析完成后向用户发送通知):

views.py:

from producer_main.celery import app as celery_app

@celery_app.task()
def _upload_error_simple(uuid):
    print uuid
    result = celery_app.AsyncResult(uuid)
    print result.backend
    print result.state
    print result.result
    print result.traceback
    msg = 'Task {0} raised exception: {1!r}\n{2!r}'.format(uuid,
                                                           result.result,
                                                           result.traceback)


class UploadNewFile(APIView):
    def post(self, request, repository_id, format=None):
        try:    
            uploaded_file = self.data['files'][self.data['files'].keys()[0]]
            self.path = default_storage.save('{0}/{1}'.format(settings.MEDIA_ROOT,
                                                              uploaded_file.name),
                                             uploaded_file)
            print type(import_file)
            self.async_result = import_file.apply_async((self.path,  request.user),
                                                        link_error=_upload_error_simple.s())


            print 'results from self.async_result:'
            print self.async_result.id
            print self.async_result.backend
            print self.async_result.state
            print self.async_result.result
            print self.async_result.traceback
            return Response()
        except (PermissionDenied, InvalidArgument, NotFound, KeyError) as ex:
            gutils.handle_exceptions(ex)

tasks.py:

from producer_main.celery import app
from utilities.general import upload_class


@app.task
def import_file(path, user):
    """Asynchronously import a course."""
    upload_class(path, user)

celery.py:

"""
As described in
http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html
"""
from __future__ import absolute_import

import os
import logging

from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'producer_main.settings')

from django.conf import settings

log = logging.getLogger(__name__)

app = Celery('producer')  # pylint: disable=invalid-name

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)  # pragma: no cover

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

我的后端是这样配置的:

CELERY_ALWAYS_EAGER = True
CELERY_EAGER_PROPAGATES_EXCEPTIONS = False
BROKER_URL = 'amqp://'
CELERY_RESULT_BACKEND = 'redis://localhost'
CELERY_RESULT_PERSISTENT = True
CELERY_IGNORE_RESULT = False

当我针对 link_error 状态运行单元测试时,我得到:

Creating test database for alias 'default'...
<class 'celery.local.PromiseProxy'>
130ccf13-c2a0-4bde-8d49-e17eeb1b0115
<celery.backends.redis.RedisBackend object at 0x10aa2e110>
PENDING
None
None
results from self.async_result:
130ccf13-c2a0-4bde-8d49-e17eeb1b0115
None
FAILURE
Non .zip / .tar.gz file passed in.
Traceback (most recent call last):

所以任务结果在我的_upload_error_simple() 方法中是不可用的,但是它们可以从self.async_result 返回的变量中获得...

【问题讨论】:

  • _upload_error() 函数的更新版本有错误的签名 - 根据docs 它应该是def _upload_error(uuid)
  • 除非您将 _upload_error() 的部分评估签名作为 link_error 参数传递 - 如果是这样,请更新您的问题以使一切更清楚
  • 请不要只是将代码转储到最后 - 编辑问题以使其连贯
  • 对于import_file,您使用@app.task,但对于_upload_error_simple,您使用@celery_app.task
  • 它们在不同的文件中,应用程序以不同的名称导入......好的,我会清理问题。

标签: django celery


【解决方案1】:

您似乎将_upload_error() 作为类的绑定方法——这可能不是您想要的。试着让它成为一个独立的任务:

@celery_app.task(bind=True)
def _upload_error(self, uuid):
    result = celery_app.AsyncResult(uuid)
    msg = 'Task {0} raised exception: {1!r}\n{2!r}'.format(uuid,
                                                       result.result,
                                                       result.traceback)

class Whatever(object):
    ....
    self.async_result = import_file.apply_async((self.path, request.user),
                                                link=self._upload_success.s(
                                                    "Upload finished."),
                                                link_error=_upload_error.s())

事实上,不需要self 参数,因为它没有被使用,所以你可以这样做:

@celery_app.task()
def _upload_error(uuid):
    result = celery_app.AsyncResult(uuid)
    msg = 'Task {0} raised exception: {1!r}\n{2!r}'.format(uuid,
                                                       result.result,
                                                       result.traceback)

注意bind=Trueself 的缺失

【讨论】:

  • 这不起作用,它仍然在_upload_error中显示result.state = 'PENDING'result.result = None...
  • 打印出uuid,看看是否有效。您的结果后端配置正确吗?
  • 它看起来像一个有效的 uuid,我假设后端配置正确,因为我可以从我的 self.async_result 中获取结果......?第一次使用 Celery 和 RabbitMQ,将在更新中包含更多信息。
  • 你在结果后端使用什么?
  • 对于结果后端,我尝试了 RPC、AMPQ,现在尝试 Redis ...当我打印 result.backend 时,我得到类似 取决于我配置了哪一个。我想接下来是如何检查这些配置是否正确......
【解决方案2】:

我无法让linklink_error 回调工作,所以我最终不得不使用the docsthis SO question 中描述的on_failureon_success 任务方法。我的tasks.py 然后看起来像:

class ErrorHandlingTask(Task):
    abstract = True

    def on_failure(self, exc, task_id, targs, tkwargs, einfo):
        msg = 'Import of {0} raised exception: {1!r}'.format(targs[0].split('/')[-1],
                                                             str(exc))

    def on_success(self, retval, task_id, targs, tkwargs):
        msg = "Upload successful. You may now view your course."    

@app.task(base=ErrorHandlingTask)
def import_file(path, user):
    """Asynchronously import a course."""
    upload_class(path, user)

【讨论】:

    【解决方案3】:

    小心UUID 实例!

    如果您尝试获取 id 不是string 类型而是UUID 类型的任务的状态,您将只能获得PENDING 状态。

    from uuid import UUID
    from celery.result import AsyncResult
    
    task_id = UUID('d4337c01-4402-48e9-9e9c-6e9919d5e282')
    
    print(AsyncResult(task_id).state)
    # PENDING
    
    print(AsyncResult(str(task_id)).state)
    # SUCCESS
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多