I tried reading official documentation as well as other SO threads, but it is still not clear how Celery works.
From what I understand:
@shared_task decorator function defines the work to be performed.So, then the burning question is:
How does the Celery worker get the code defined in @shared_task to run the task?
Basically, how does 3. get what's defined in 1. if they are only connected using a message broker? Is the python code stored in the message broker as string? What is the data structure of the message broker item/record?
After spending some time and actually implementing Celery, I was able to figure out what's actually going on. This is perhaps a short coming of my own ways of learning or the documentation overlooks it, either way, I want to leave it here in case others are also wondering the same.
The following is completely not obvious IMO:
Django app: Celery tasks are defined here. It has Celery installed as dependency.
Message broker: A message broker gets this task from 1. and queues it.
Celery Worker: This is actually not a separate python app that only contains Celery. It is a full blown instance of 1.. So you launch Django twice, one is producing tasks and the other is working on them. Both containing the same @shared_task function.
Caveats that the documentation should explain:
@shared_task function.celery -A "your_app" workers -l INFO. It's not running Gunicorn, but still contains all the code in 1.I think right off the bat in the introduction of Celery, they should explain that both the producer and worker instances contain your source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With