1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. In that case, how to get the status and result without depending on this hidden connection? I tried from celery.result import AsyncResult task='a158ce19-54dc-4d40-b6a4-b7732736537d' status: str = AsyncResult(id=task, backend=REDIS_BROKER).state result: int = AsyncResult(id=task, backend=REDIS_BROKER).result But it throws error. Does AynchResult need the entire app or the task id and backend URL is enough? What's the minimal coupling I can use here, without depending on the current_app?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.