I tried reading official documentation as well as other SO threads, but it is still not clear how Celery works.
From what I understand:
- Django app: Celery is installed in Django (or any app) where
@shared_task
decorator function defines the work to be performed. - Message broker: A message broker gets this task from 1. and queues it.
- Celery Worker: A completely separate Celery worker picks up the task and runs it. This worker can be in a completely different machine even, so long as it has access to the message broker.
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?