views:

226

answers:

2

I need to run a CPU- and memory-heavy Python script (analyzing and altering a lengthy WAV file) as a background process on my web server (a VPS), between HTTP requests.

The script takes up to 20 seconds to run and I am concerned about the performance on my server. Is there a good approach to either lower the priority of the process, periodically cede control to the OS, or otherwise protect the performance of my modest server?

+6  A: 

Assuming it's a UNIX server, you could use the nice command to lower its priority. That should do the trick.

Eric Petroelje
Python conveniently provides os.nice(...)
timday
nice! bad pun but such is life.
Kurt
+4  A: 

You can use cpulimit on a linux based server. It will allow you to limit the CPU usage (specify the limit as a percentage) even of scripts that have already started running, and its usage is pretty straightforward.

It's available on the Debian repository, so you can install it easily using aptitude:

apt-get install cpulimit

Typical ways to use cpulimit includes:

# To limit CPU usage to 75% of program called foo:
cpulimit -e foo -l 75

# To limit CPU usage to 50% of program with pid = 1582
cpulimit -p 1582 -l 50
Wadih M.