Nicing a running python process?

AME picture AME · Dec 18, 2012 · Viewed 15.8k times · Source

When my longer-running programm starts, I want to lower its priority so it does not consume all resources avaiable on the machine it runs. Circumstances make it necessary that the programm limits itself.

Is there a nice-like python-command I could use so that the programm does not utilize the full capacity of the computer it is running on?

Answer

Petesh picture Petesh · Dec 18, 2012

you can always run the process with nice pythonscript,

but if you want to set the nice-level within the script you can do:

import os
os.nice(20)

You could progressively increment the nice level the longer the script is running, so it uses less and less resources over time, which is a simple matter of integrating it into the script.

Alternatively, from outside the script, once it's running you should be able to use renice -p <pid>