How to route a chain of tasks to a specific queue in celery?

前端 未结 3 1129
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 07:21

When I route a task to a particular queue it works:

task.apply_async(queue=\'beetroot\')

But if I create a chain:

chain = t         


        
3条回答
  •  天命终不由人
    2020-12-31 08:02

    This is rather late, but I don't think the code provided by @mpaf is entirely correct.

    Context: In my case, I have two subtasks, out of which the first provides a return value which is passed on to the second as the input argument. I was having trouble in getting the second task to execute - I saw in the logs that Celery would acknowledge the second task as a callback of the first, but it would never execute the second.

    This was my non-working chain code -:

    from celery import chain
    
    chain(
        module.task1.s(arg),
        module.task2.s()
    ).apply_async(countdown=0.1, queue='queuename')
    

    Using the syntax provided in @mpaf's answer, I got both tasks to execute, but the execution order was haphazard and the second subtask was not acknowledged as a callback of the first. I got the idea to browse the docs on how to explicitly set a queue on a subtask.

    This is the working code -:

    chain(
        module.task1.s(arg).set(queue='queuename'),
        module.task2.s().set(queue='queuename')
    ).apply_async(countdown=0.1)
    

提交回复
热议问题