简体   繁体   English

以较低的CPU优先级在后台运行任务

[英]Running tasks in the background with lower CPU priority

I have a feature in my CMS that allows a user to upload a zip file full of images and the server will extract them and insert each one into an image gallery. 我的CMS中有一项功能,该功能允许用户上传一个包含完整图像的zip文件,服务器将提取它们并将其插入到图片库中。 I've noticed that this grinds up the CPU quite severely and causes other requests to slow down. 我注意到,这会严重占用CPU并导致其他请求变慢。

I'm thinking of using the delayed_job plugin to delegate each image addition into the background, but I also want to give that process a lower CPU priority so that it doesn't bog down the server. 我正在考虑使用delay_job插件将每次添加的图像委派给后台,但是我也想给该进程分配较低的CPU优先级,以免使服务器瘫痪。

I'm pretty confident in the delaying part of the exercise, but the throttling part is where I'm stuck. 我对练习的延迟部分很有信心,但节流部分是我遇到的困难。 Is there a ruby way of lowering the priority of a method call? 有没有一种降低方法调用优先级的方法?

It's the image resizing that causes the CPU chew. 图像调整大小会导致CPU损坏。

Any ideas welcome :) 任何想法欢迎:)

If your CMS is running on linux system, then you can do this using the "nice" command. 如果CMS在Linux系统上运行,则可以使用“ nice”命令执行此操作。 "nice" will start a process at a lower priority. “ nice”将以较低的优先级启动进程。 Easiest way to use it is to just put nice in front of your command. 最简单的使用方法是在命令前面加上漂亮的文字。 So if you were running the command like 因此,如果您正在运行以下命令

unzip uploaded-images.zip

instead run 而是运行

nice unzip uploaded-images.zip

This will cause the unzip process to get lower CPU priority, letting other processes run first. 这将导致解压缩进程获得较低的CPU优先级,从而让其他进程先运行。 See the man page for more options, like how to adjust the priority level. 有关更多选项,例如如何调整优先级,请参见手册页

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM