[英]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.