【问题标题】:How to return the result of the first task from a celery chain?如何从芹菜链返回第一个任务的结果?
【发布时间】:2013-01-15 15:50:33
【问题描述】:

这些是我的任务:

@task(name = 'hello')
def hello():
    print "hello"
    return "helo"

@task(name = 'hey')
def hey(resp):
    print resp

我这样称呼他们:g = celery.chain(hello.s(),hey.s()) 但我希望它像这样完成:你好任务应该返回一个值,不仅是任务“嘿”,它还应该返回一个值。我的意思是,一旦完成执行,我应该能够获得“hello”的返回值。怎么做?

【问题讨论】:

    标签: python django celery


    【解决方案1】:

    调用链时返回的结果实例将是链中的最后一个任务,但它会保留对父级的引用,因此您可以遍历父级以获取第一个任务:

    r = chain(hello.s(), hey.s())()
    
    r.parent.get(timeout=1)
    r.parent.parent.get(timeout=1)
    
    first = r
    while first.parent:
        first = first.parent
    

    见http://docs.celeryproject.org/en/latest/userguide/canvas.html#chains

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多