Search code examples
pythoncelerydjcelery

Celery: How '|' operator works while chaining multi tasks?


I know | is a bitwise 'Or' operator but it makes me wonder that how it works in case of celery while chaining multiple tasks.

(first_task.s(url) | second_tasks.s()).apply_async()

I know that second task would take result of the first function as args but how's that possible? Where is '|' overloading in dj-celery source code?

@task
def second_task(results):
   do_something(results)

Can somebody please provide some insights?


Solution

  • As mentioned above, Celery overrides the __or__ operator, specifically as follows:

    def __or__(self, other):
        if isinstance(other, group):
            other = maybe_unroll_group(other)
        if not isinstance(self, chain) and isinstance(other, chain):
            return chain((self, ) + other.tasks, app=self._app)
        elif isinstance(other, chain):
            return chain(*self.tasks + other.tasks, app=self._app)
        elif isinstance(other, Signature):
            if isinstance(self, chain):
                return chain(*self.tasks + (other, ), app=self._app)
            return chain(self, other, app=self._app)
        return NotImplemented
    

    The full implementation is here: https://github.com/celery/celery/blob/master/celery/canvas.py#L324