How Does Celery AsyncResult Function Know Which Broker or Backend to Query?

235 Views Asked by At

This is how I am sending a task to the celery workers and capturing the resulting task id. The app variable, i.e. the celery app, has the redis broker and backend url set as part of its properties. This is working fine.

from celery import Celery
REDIS_BROKER:str='redis://127.0.0.1:6369'
app: Celery = Celery(backend=REDIS_BROKER, broker=REDIS_BROKER)
task: str = app.send_task(name='add_two_numbers',
                                      args=[28, 93]).id
# task='574b72ad-4512-4e0b-a14b-f56a2f725374' # An example

When I check the task status and result, this also works, when I thought it should not (which is why I am asking the question).

from celery.result import AsyncResult
status: str = AsyncResult(id='574b72ad-4512-4e0b-a14b-f56a2f725374').state
result: int = AsyncResult(id='574b72ad-4512-4e0b-a14b-f56a2f725374').result

My question is why this works? There can be any number of redis brokers running on any IP address, which is why I am supplying the socket address of the broker to the app variable. Should not AsyncResult ask for task id, broker URL and backend URL to fetch the task status/result for me? Or does it have some kind of invisible connection with the app object in the script to get those broker and backend URLs?

When I look up, it seems most tutorials and answers couple django and celery together to answer the questions, but I am using celery without django, purely as a task processor without any web development component. Hence, the question.

1

There are 1 best solutions below

1
Lemon Reddy On

You must have initialised app before calling AsyncResult and using it. Celery has a current_app proxy variable which is resolved to the current instance of app in the current session/runtime. As you have guessed, there is some connection to the app instance inside AsyncResult through current_app reference.

If you ran it in a python REPL session, launch a new terminal and test. This would throw an exception of Backend not configured properly.