为什么要运行Celery的计划任务比crontab更好? [英] Why would running scheduled tasks with Celery be preferable over crontab?

查看:903
本文介绍了为什么要运行Celery的计划任务比crontab更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到Celery已经是运行任务队列的堆栈的一部分(即它不是被添加只是为了运行crons,这似乎是一个过度的IMHO)。



它的周期性任务功能如何有助于替代crontab?
具体寻找以下几点。




  • crontab的主要优缺点

  • 使用芹菜比crontab更好的选择的案例

  • Django具体用例:芹菜与crontab一起运行基于django的定期任务,芹菜已被包含在堆栈中作为 django为了排队django任务,请使用-celery


解决方案

一直在生产网站使用cron,并在当前的项目中切换到芹菜。
我比芹菜更芹菜,这就是为什么:




  • 芹菜+芹菜芽比颗粒度比cron更细。 Cron不能每分钟运行一次,而芹菜可以(每隔90秒运行一次任务,检查电子邮件队列发送消息,另一个清除在线用户列表)。

  • cron行必须使用绝对路径和用户信息调用脚本或唯一命令。芹菜调用python函数,不需要编写代码。

  • 芹菜要部署到另一台机器,你通常只需要拉/复制你的代码,这通常在一个地方。与cron部署将需要更多的工作(您可以自动化但是...)

  • 我真的发现芹菜比cron更适合日常清洁(缓存,数据库),一般来说,简短的任务。然而,转储数据库对于cron来说更为重要,因为您不希望用太长的任务来混淆事件队列。

  • 最终,Celery容易在机器间分布。 / li>

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.

解决方案

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.

这篇关于为什么要运行Celery的计划任务比crontab更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆