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. I've noticed that this grinds up the CPU quite severely and causes other requests to slow down.
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.
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 prio开发者_C百科rity of a method call?
It's the image resizing that causes the CPU chew.
Any ideas welcome :)
If your CMS is running on linux system, then you can do this using the "nice" command. "nice" will start a process at a lower priority. 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. See the man page for more options, like how to adjust the priority level.
精彩评论