简体   繁体   English

Celery:在另一个任务之后执行一个任务,但返回第一个任务的结果

[英]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 ) 有时我需要在任务A之后运行任务B,但是返回任务A的结果。查看文档,我尝试过( 避免启动同步子任务

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. B没有任何参数。 It does not accept the result of A 它不接受A的结果
  • B must be executed after A completes B必须 A完成之后执行
  • I do not need to wait for B to complete, and I do not need its result (B must be async?) 我不需要等待B完成,也不需要它的结果(B必须异步吗?)
  • How do I return the result of A in do_all? 如何在do_all中返回A的结果? (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. 但是另一种可行的方法是,在任务A中使用on_successafter_return处理程序来启动任务B。如果还使用ignore_result选项,则对任务B的调用ignore_resultignore_result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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