在CeleryPython中,一个任务输出到并行处理任务

In Celery Python, output of a task to parallel processing task

我创建了一个任务

@app.task
def mainTask(msg,nc):
    decryptFunction.decryptFunc(msg)
    if len(decryptFunction.messageJson):

我正在异步接收数据。

def on_message(client, userdata, msg):
    result = mainTask.delay(msg.payload.decode("utf-8"),1)

我需要在 5 个任务中同时使用输出 decryptFunction.messageJson 和 运行

我从你的要求中了解到,我建议,而不是将 mainTask 设为异步,你可以将其他 5 个任务称为 celery 任务,来自 mainTask 即,

def mainTask(msg, nc):
     decryptFunction.decryptFunc(msg)
     if len(decryptFunction.messageJson):
         response1 = task1.delay(decryptFunction.messageJson)
         response2 = task2.delay(decryptFunction.messageJson)
         response3 = task3.delay(decryptFunction.messageJson)
         response4 = task4.delay(decryptFunction.messageJson)
         response5 = task5.delay(decryptFunction.messageJson)

而您的 on_message 功能为:

def on_message(client, userdata, msg):
    result = mainTask(msg.payload.decode("utf-8"),1)

但这是假设您的其他 5 个任务是相互排斥的,并且其中一个的响应不会影响另一个任务,因为如果它们不是,则您不能将它们设为异步任务。

但如果您的 mainTask 很耗时,您可以将其用作 Celery 任务。