Celery Task Chain and Accessing **kwargs

后端 未结 3 1071
夕颜
夕颜 2020-12-29 11:38

I have a situation similar to the one outlined here, except that instead of chaining tasks with multiple arguments, I want to chain tasks that return a dictionary with multi

3条回答
  •  北海茫月
    2020-12-29 11:50

    This is my take at the problem, using an abstract task class:

    from __future__ import absolute_import
    from celery import Task
    from myapp.tasks.celery import app   
    
    
    class ChainedTask(Task):
        abstract = True    
    
        def __call__(self, *args, **kwargs):
            if len(args) == 1 and isinstance(args[0], dict):
                kwargs.update(args[0])
                args = ()
            return super(ChainedTask, self).__call__(*args, **kwargs)
    
    @app.task(base=ChainedTask)
    def task1(x, y):
        return {'x': x * 2, 'y': y * 2, 'z': x * y}    
    
    
    @app.task(base=ChainedTask)
    def task2(x, y, z):
        return {'x': x * 3, 'y': y * 3, 'z': z * 2}
    

    You can now define and execute your chain as such:

    from celery import chain
    
    pipe = chain(task1.s(x=1, y=2) | task2.s())
    pipe.apply_async()
    

提交回复
热议问题