首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >芹菜:如果文档有错误,如何使用"link_error“?

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

Stack Overflow用户
提问于 2015-06-13 20:34:54
回答 1查看 3.2K关注 0票数 0

在芹菜文档中: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作为参数: 从__future__导入print_function从proj.celery导入应用程序@app.task def log_error(task_id):@app.task= app.AsyncResult(task_id) result.get(propagate=False) #确保结果被写入。使用open(‘/var/os.path.join’,task_id),‘a’作为fh: print(‘-\n {1} {2}'.format( task_id,result.result,result.traceback),file=fh)

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

代码语言:javascript
复制
/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))
EN

回答 1

Stack Overflow用户

发布于 2015-06-13 23:03:14

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

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

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

代码语言:javascript
复制
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('--\n\n{0} {1} {2}'.format(
                task_id, result.result, result.traceback), file=fh)

注释: result.resultZeroDivisionError的例子中是字典:

代码语言:javascript
复制
{
    'exc_type': 'ZeroDivisionError',  # Name of Exception type
    'exc_message': 'division by zero'  # Reason of exception
}

注:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30823283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档