Why would running scheduled tasks with Celery be preferable over crontab?

2024/11/20 23:29:04

Considering Celery is already a part of the stack to run task queues (i.e. it is not being added just for running crons, that seems an overkill IMHO ).

How can its "periodic tasks" feature be beneficial as a replacement for crontab ? Specifically looking for following points.

  • Major pros/cons over crontab
  • Use cases where celery is better choice than crontab
  • Django specific use case: Celery vs crontab to run django based periodic tasks, when celery has been included in the stack as django-celery for queing django tasks.
Answer

I've been using cron for a production website, and have switched to celery on a current project. I'm far more into celery than cron, here is why:

  • Celery + Celerybeat has finer granularity than cron. Cron cannot run more than once a minute, while celery can (I have a task run every 90 seconds which checks an email queue to send messages, and another which cleans the online users list).
  • A cron line has to call a script or a unique command, with absolute path and user info. Celery calls python functions, no need to write more than code.
  • With celery, to deploy to another machine, you generally just have to pull/copy your code, which is generally in one place. Deploying with cron would need more work (you can automate it but...)
  • I really find celery better suited than cron for routine cleaning (cache, database), and in general, for short tasks. Dumping a database is more a work for cron, however, because you don't want clutter the event queue with too long tasks.
  • Not the least, Celery is easily distributed across machines.
https://en.xdnf.cn/q/26265.html

Related Q&A

use a css stylesheet on a jinja2 template

I am making a website using html, css, flask and jinja2.I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in.How would I li…

How to extend Python class init

I have created a base class:class Thing():def __init__(self, name):self.name = nameI want to extend the class and add to the init method so the that SubThing has both a name and a time property. How d…

type hint for an instance of a non specific dataclass

I have a function that accepts an instance of any dataclass. what would be an appropriate type hint for it ?havent found something official in the python documentation this is what I have been doing, …

Is there a static constructor or static initializer in Python?

Is there such a thing as a static constructor in Python? How do I implement a static constructor in Python?Here is my code... The __init__ doesnt fire when I call App like this. The __init__ is not…

What does this - in jinja2 template engine do?

I am learning jinja2 because Google App Engine recommends it.I found this example on Wikipedia: http://en.wikipedia.org/wiki/Jinja_%28template_engine%29{%- for item in item_list %}{{ item }}{% if not l…

When to apply(pd.to_numeric) and when to astype(np.float64) in python?

I have a pandas DataFrame object named xiv which has a column of int64 Volume measurements. In[]: xiv[Volume].head(5) Out[]: 0 252000 1 484000 2 62000 3 168000 4 232000 Name: Volume, d…

How to change folder names in python?

I have multiple folders each with the name of a person, with the first name(s) first and the surname last. I want to change the folder names so that the surname is first followed by a comma and then t…

Python return list from function

I have a function that parses a file into a list. Im trying to return that list so I can use it in other functions. def splitNet():network = []for line in open("/home/tom/Dropbox/CN/Python/CW2/net…

Python Json loads() returning string instead of dictionary?

Im trying to do some simple JSON parsing using Python 3s built in JSON module, and from reading a bunch of other questions on SO and googling, it seems this is supposed to be pretty straightforward. Ho…

Sort dataframe by string length

I want to sort by name length. There doesnt appear to be a key parameter for sort_values so Im not sure how to accomplish this. Here is a test df:import pandas as pd df = pd.DataFrame({name: [Steve, Al…