I'm currently going through the Celery user guide which has the following example:
# ((4 + 16) * 2 + 4) * 8
>>> c2 = (add.s(4, 16) | mul.s(2) | (add.s(4) | mul.s(8)))
>>> res = c2()
>>> res.get()
352
But this example only uses one partial result at a time. My question is whether it is possible to use several, with the same primitive tasks (add and mul), e.g.:
# ((4 + 16) * (2 + 4)) * 8
>>> c2 = ???
>>> res = c2()
>>> res.get()
960
Below are my tries so far:
# TypeError: mul() missing 1 required positional argument: 'y'
>>> c2 = (add.s(4, 16) | (add.si(2, 4) | mul.s()) | mul.s(8))
# Works, but needs to implement a new "prod" task
>>> c2 = (group(add.s(4, 16), add.s(2, 4)) | prod.s() | mul.s(8))
Where prod is simply:
@app.task
def prod(xs):
return functools.reduce((lambda x, y: x * y), xs)