简体   繁体   中英

How to return the result of the first task from a celery chain?

These are my tasks:

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

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

I'm calling them like this: g = celery.chain(hello.s(),hey.s()) But I want it to be done like this: hello task should return a value to not only to the task "hey" it should also a return a value back. By that I mean, I should be able to get the return value of "hello" once it is done executing. How to do it?

The result instance returned when you call a chain will be for the last task in the chain, but it will keep a reference back to the parent, so you can traverse the parents to get the first task:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM