芹菜:如果文档有错误,如何使用"link_error"?



在 Celery 文档中:http://celery.readthedocs.org/en/latest/userguide/canvas.html#chains 如何使用link_error的示例:

您还可以使用 link_error 参数添加错误回调:

add.apply_async((2, 2), link_error=log_error.s())
add.subtask((2, 2), link_error=log_error.s())

由于异常只能在使用泡菜时序列化,因此错误 回调将父任务的 ID 作为参数:

from __future__ import print_function 
import os 
from proj.celery import app
@app.task 
def log_error(task_id):
    result = app.AsyncResult(task_id)
    result.get(propagate=False)  # make sure result written.
    with open(os.path.join('/var/errors', task_id), 'a') as fh:
        print('--nn{0} {1} {2}'.format(
            task_id, result.result, result.traceback), file=fh)

但是在这个例子中是错误的,因为他们调用了AsuncResult.get内部任务,这会导致DEADLOCK和以下日志条目:

/opt/.virtualenvs/spark/lib/python3.4/site-packages/celery/result.py:45: RuntimeWarning: Never call result.get() within a task!
See http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
In Celery 3.2 this will result in an exception being
raised instead of just being a warning.
  warnings.warn(RuntimeWarning(E_WOULDBLOCK))
[2015-06-13 20:30:19,242: WARNING/Worker-4] /opt/.virtualenvs/spark/lib/python3.4/site-packages/celery/result.py:45: RuntimeWarning: Never call result.get() within a task!
See http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
In Celery 3.2 this will result in an exception being
raised instead of just being a warning.
  warnings.warn(RuntimeWarning(E_WOULDBLOCK))

请执行此检查列表以确保配置正常:

  1. CELERY_IGNORE_RESULT必须设置为False
  2. 必须设置CELERY_BACKEND_RESULT,例如 'amqp'(兔子MQ)
  3. @task装饰器不得在每个任务上都有选项ignore_result=True(但您可以创建不可变的签名)

最后一个是正确修改的获取结果(allow_join_result)的任务:

from celery.result import allow_join_result
@app.task
def log_error(task_id):
    with allow_join_result():
        result = app.AsyncResult(task_id)
        result.get(propagate=False)  # make sure result written.
        with open(os.path.join('/var/errors', task_id), 'a') as fh:
            print('--nn{0} {1} {2}'.format(
                task_id, result.result, result.traceback), file=fh)

注意: 在这种情况下result.result ZeroDivisionError是字典:

{
    'exc_type': 'ZeroDivisionError',  # Name of Exception type
    'exc_message': 'division by zero'  # Reason of exception
}

注意:

这是#2652问题,当AsyncResult芹菜忽略错误时调用get()

相关内容

  • 没有找到相关文章

最新更新