简体   繁体   English

我应该使用普通的Python代码还是Celery? Django的。

[英]Should I use plain Python code or Celery? Django.

I have a heavy function (a lot of calculations are done) which outputs a individual number for each user in my Django project. 我有一个繁重的函数(完成了大量计算),该函数为Django项目中的每个用户输出一个单独的数字。 This number changes just a little over time so to minimize the server load I thought about running the function once a day, save the output and just reference the output. 这个数字随时间变化很小,因此为了最大程度地减少服务器负载,我考虑每天运行一次该功能,保存输出并仅引用输出。 I know that these kinda things are usually handled with Celery but the package requires a lot of site packages and extra modules so I thought about writing a simple function like: 我知道这些事情通常是用Celery处理的,但是该程序包需要很多站点程序包和额外的模块,因此我考虑编写一个简单的函数,例如:

x0 = #last.time function was called
x1 = datetime.now
if x0-x1 > 1 day:
  def whatever():
    ....
    x0 = datetime.now
    return ....

I like to keep my code clean and not to install Packages which are not really required so I would like to know if there are any downsides by "just" using Python or any gain when I would do that with Celery . 我想保持我的代码整洁,而不要安装不是真正不需要的Packages,所以我想知道使用Python“仅”使用它是否有不利之处,或者当我使用Celery这样做时是否有任何好处。 The task does not need to be asynchronous so I don't care about that. 该任务不需要是异步的,所以我不在乎。 Is there a clear "Use case" when Celery should be used and when not? 是否应该使用芹菜,何时不应该使用明确的“用例”? Is there a performance loss/gain? 有绩效损失/收益吗?

I hope somebody can explain that properly. 我希望有人能正确解释。

Celery is a clear winner but I would like to explain this with pros and cons. 芹菜显然是赢家,但我想用优点和缺点来解释一下。

Pros : 优点

  • You can control celery from Django very easily. 您可以从Django轻松控制芹菜。 Running a celery task, cancelling task, checking state/progress of task can be done within django. 运行芹菜任务,取消任务,检查任务状态/进度可以在Django中完成。

  • A periodical task running with celery is very simple, just register the task from django run the celery worker and voila you are done. 用芹菜运行一个定期任务非常简单,只需注册django的任务即可运行芹菜工人,瞧,就完成了。 No need to mess around with crontab or background processes. 无需弄乱crontab或后台进程。

  • Celery is very easy to setup and run. Celery非常易于设置和运行。 You might already know that if you have gone through the introduction of celery. 您可能已经知道,如果您已经完成了芹菜的介绍。

Cons 缺点

  • One of the cons is that you need to have at least one result backend with either redis, rabbitmq or any other one running with celery for queuing purposes. 缺点之一是,您需要至少一个带有redis,rabbitmq或其他任何与celery一起运行的结果后端以用于排队。 Although RabbitMq is not a heavy you need to install it once. 尽管RabbitMq并不繁重,但您需要安装一次。

  • One more is that celery worker itself takes some memory but that won't be an issue if you are on a server, on local memory consumption might seem high to you. 另外一个问题是celery worker本身会占用一些内存,但是如果您在服务器上,这将不是问题,因为本地内存消耗对您来说似乎很高。

I would suggest celery because it would provide you more control over your task rather than a simple background process. 我建议使用芹菜,因为它可以为您提供对任务的更多控制,而不是简单的后台流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该将Celery或Carrot用于Django项目吗? - Should I use Celery or Carrot for a Django project? 我正在尝试将 celery 应用于 django 中基于 Class 的视图(apis)。 我怎样才能做到这一点? - I'm trying to apply celery to Class based view(apis) in django. How can I do this? ImageField django。 如何使用? - ImageField django. How to use? 如何控制python / Django / Celery代码质量 - How to control of the python/Django/Celery code quality 如何编写代码,以便在带有django的模型中仅应选择一个单选按钮? - How to write code such that only one of the radio button should be selected in models with django.? 我可以在没有 django 的情况下使用芹菜吗 - Can i use celery without django Django的。 在代码文件夹中使用.ini文件 - Django. Using .ini files in code folder Pyhtho Django。 messaa 来自 python 文件,我想在 HTML 中显示它,他的代码正在运行,但我希望消息显示为弹出或警报 - Pyhtho Django. the messaa is from python file and i want to show it in HTML his code is working but I want the message will show as pop up or alert Django的。 签入模板或使用额外的字段? - Django. Check in template or use an extra field? 我应该在我的 Django 应用程序中使用 Celery/Redis 吗? - Should I be using Celery/Redis in my Django application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM