3

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?

Humble Learner
  • 791
  • 12
  • 26

2 Answers2

2

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

Elliot
  • 51
  • 2
1

They are likely using operator overloading as __or__(self, other): http://www.rafekettler.com/magicmethods.html

I don't know the implementation details of Celery, but just to give you an idea:

class Task(object):
    def __init__(self, name):
        self.name = name
        self.chain = [self]

    def __or__(self, other):
        self.chain.append(other)
        return self

    def __repr__(self):
        return self.name

    def apply_async(self):
        for c in self.chain:
            print "applying", c


(Task('A') | Task('B') | Task('C')).apply_async()

Output:

applying A
applying B
applying C
synthomat
  • 774
  • 5
  • 11