简体   繁体   中英

Celery: Executing a task after another task, but return the result of the first task

Sometimes I need to run task B after task A, but return the result of task A. Looking at the docs, I have tried to ( Avoid launching synchronous subtasks )

So I would use a chain, like this:

@celery.task
def A():
    return 5

@celery.task
def B():
    return 2

def do_all():
    chain = A.s() | B.s()
    chain()
    return result_of_A

But this would not work. In my case I need:

  • B does not have any parameter. It does not accept the result of A
  • B must be executed after A completes
  • I do not need to wait for B to complete, and I do not need its result (B must be async?)
  • How do I return the result of A in do_all? (A must be sync?)

Is it possible to implement this with chains or any other subtasks primitives?

I can't noodle a way to do this with primitives; there probably is one. But a different approach that would work is, use the on_success or after_return handler in task A to initiate task B. If you also use the ignore_result option, then the invocation of task B would be truly fire-and-forget.

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