简体   繁体   English

如何从 Python Celery 中的另一个任务触发任务?

[英]How can I trigger tasks from another task in Python Celery?

# get a list of stuff
@celery.task
def getList():
    listOfStuff = getStuff()
    for thing in listOfStuff:
        processThing.apply_async(args=(thing))


# another attempt at list of stuff
@celery.task
def getList():
    listOfStuff = getStuff()
    for thing in listOfStuff:
        processThing.s((thing))

@celery.task
def processThing(thing):
    pass

So neither getList() implementation triggers the processThing tasks.所以 getList() 实现都不会触发 processThing 任务。 I can't figure out why.我不知道为什么。 I'm guessing there is a better way of accomplishing what I'm trying to accomplish but I can't figure out what that is.我猜想有更好的方法来完成我想要完成的事情,但我不知道那是什么。

How can I kick off tasks from another task?我怎样才能从另一个任务开始任务?

This situation is explained in the docs under http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks http://docs.celeryproject.org/en/latest/userguide/tasks.html#avoid-launching-synchronous-subtasks下的文档中对此情况进行了说明

Here's a previous thread that shows some code on a similar situation: How to chain a Celery task that returns a list into a group? 这是先前的线程,它显示了类似情况下的一些代码: 如何链接将清单返回到组的Celery任务?

celery.execute.send_task("task.fqn", args=[], kwargs={})

这是用来产生任务的命令。

Celery does not require access to a task's code base in order to invoke it. Celery 不需要访问任务的代码库即可调用它。 The trick is to invoke a task by its name, either directly via celery_app.send_task(...) or by creating a Signature object celery_app.signature(...) which is the equivalent of calling task.s(...) when you do have access to the task's code base.诀窍是直接通过celery_app.send_task(...)或通过创建 Signature object celery_app.signature(...)调用任务名称,这相当于调用task.s(...)当您确实有权访问任务的代码库时。

more details : https://www.distributedpython.com/2018/06/19/call-celery-task-outside-codebase/更多详细信息https://www.distributedpython.com/2018/06/19/call-celery-task-outside-codebase/

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

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