Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django and celery on different servers and celery being able to send a callback to django once a task gets completed

I have a django project where I am using celery with rabbitmq to perform a set of async. tasks. So the setup i have planned goes like this.

  1. Django app running on one server.
  2. Celery workers and rabbitmq running from another server.

My initial issue being, how to do i access django models from the celery tasks resting on another server?

and assuming I am not able to access the Django models, is there a way once the tasks gets completed, i can send a callback to the Django application passing values, so that i get to update the Django's database based on the values passed?

like image 559
DeadDjangoDjoker Avatar asked Aug 18 '16 11:08

DeadDjangoDjoker


People also ask

How does Django Celery beat work?

We can configure periodic tasks either by manually adding the configurations to the celery.py module or using the django-celery-beat package which allows us to add periodic tasks from the Django Admin by extending the Admin functionality to allow scheduling tasks.

Can I use Celery without Django?

Yes you can. Celery is a generic asynchronous task queue.


1 Answers

Concerning your first question, accessing django models from the workers' server:

Your django app must be available on both Server A (serving users) and Server B (hosting the celery workers)

Concerning your second question, updating the database based on the values. Do you mean the result of the async task? If so, then you have two options:

  • You can just save whatever you need to save from within the task itself, assuming you have access to the database.
  • You could use a results backend (one of which is through the Django ORM) as mentioned in the official documentation of Celery about Keeping Results
like image 75
Omar Abou Mrad Avatar answered Oct 30 '22 19:10

Omar Abou Mrad