There's a caveat when getting the result the way you're doing it.
A chain is a bunch of tasks linked together, when you do `resp = c.delay()` you are queuing all the tasks in the chain
The object that `c.delay()` returns is not a pointer to the entire chain but a pointer to the last task int he chain.
Meaning, `resp` ends up with a pointer to the result of the last task in the chain, in this case the second `ok.s()`.
This means that if the middle task fails and you try to do `resp.get()` it will timeout because the task that `resp` is pointing to never actually got executed.
This also explains the behavior you're seeing for `fails_in_the_end` because `resp` is a pointer to `fail.s()` when you do `resp.get()` Celery will raise the error that happened in the task. This is how Celery handles accessing results of tasks which raised errors and there's no error handling in the task.
Now, depending on what you actually want to do there's a few different options you can do. In order of recommendation.
1) You can add success and error handlers to the chain to make sure you're not working with an incomplete chain.
@app.task(bind=True)
def success(self, result):
print(f"Chain result: ${result}")
print(f"Chain: ${self.chain}")
@app.task(bind=True)
def error(self, *args, **kwargs)
print(f"args: ${args)")
print(f"kwargs: ${kwargs}")
if __name__ == "__main__":
fails_in_the_middle = (ok.s(1) | fail.s() | ok.s() | success.s()).on_error(error.s())
fails_in_the_end = (ok.s(1) | fail.s() | success.s()).on_error(error.s())
2) Access chain results as a graph using `resp.collect(intermediate=True)`
`collect()` returns all the results in the chain as a directed acyclical graph (DAG). It's usually easier to transform this into a list so you can loop through each one of the results in order:
resp = c.delay()
for result in list(resp.collect(intermediate=True)):
print(result.get())
3) Walk the chain graph backwards until you get to the parent and loop through the children to get the results.
Since what you end up with in `resp` is actually the last task in the chain you can walk the graph backwards until the first task in the chain and get the results from there:
resp = c.delay()
parent = resp.parent
while parent is not None:
parent = resp.parent
for child in parent.children:
child.get()
Ing. Josue Balandrano Coronel