Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery-Django: Celery vs django management commands

I work for a travel company where we need to send periodic mails to our teams .

Since now I have been using django management commands and running them using crontabs. I was reading about what celery can do, but I am finding it really hard to understand why should I use celery ?

Also since it would be another addition to my django project and also to the database ,does it slow down the performance ?

like image 323
Deepankar Bajpeyi Avatar asked Jun 10 '13 09:06

Deepankar Bajpeyi


People also ask

What is Django management command?

Basically, a Django management command is built from a class named Command which inherits from BaseCommand. 1) help: It tells what actually the command does. Run the following command and see the help. python manage.py stats --help.

What is Celery with Django?

Celery is a task queue/job queue based on distributed message passing. It is focused on real-time operation, but supports scheduling as well. The execution units, called tasks, are executed concurrently on a single or more worker servers.

What is Celery Python used for?

Celery is an open-source Python library which is used to run the tasks asynchronously. It is a task queue that holds the tasks and distributes them to the workers in a proper manner. It is primarily focused on real-time operation but also supports scheduling (run regular interval tasks).

What is Shared_task in Celery?

The "shared_task" decorator allows creation of Celery tasks for reusable apps as it doesn't need the instance of the Celery app. It is also easier way to define a task as you don't need to import the Celery app instance.


1 Answers

I don't think you should use celery, Cron still sounds fine to me in your case, but you might want to give Celery a try.

To me, Celery is a Python module for [asynchronous] [distributed] task queues. It allows you to dispatch lengthy tasks to multiple processes running on multiple machines (but one process on one machine is still fine). When you need to do something that takes time (like generate thumbnails, speak to an external API or generate complex reports), you can use Celery to do this in the background without blocking HTTP request for your user.

Some advantages of Celery over crontab:

  • you can run tasks asynchronously, exactly when there is at least one celery worker free
  • it does scale well to multiple processes / machines
  • celerybeat is like crontab; but you can schedule tasks at given datetime or intervals using python syntax in your settings.py
  • you can apply rate limits (for example for some sort of prioritization)
  • there are monitoring tools like Flower which will give you a decent idea of what tasks have failed and what have succeeded

Some disadvantages of Celery:

  • setup can take some time - you have to setup queue broker and demonize the workers in production; cron will already be there
  • each worker process is likely to use ~same amount of RAM as your Django process, this can cost you $ or you may simply not have enough RAM to run Celery on, say, AWS free tier

And also, if it's just about sending e-mails, you could consider using a paid service such as Postmark (I am not affiliated to them), which will handle e-mail throttling for you.

like image 76
Peter Kilczuk Avatar answered Sep 23 '22 23:09

Peter Kilczuk