Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 priority of a method call?

It's the image resizing that causes the CPU chew.

Any ideas welcome :)

like image 751
Brendon Muir Avatar asked Mar 08 '10 03:03

Brendon Muir


1 Answers

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.

like image 178
muudscope Avatar answered Oct 24 '22 03:10

muudscope