Inheriting a running python process?

When my more running program starts, I want to lower its priority so that it does not consume all the resources available on the machine on which it is running. Circumstances make it necessary for a program to limit itself.

Is there a nice python command that I could use so that the program does not use the full capacity of the computer on which it works?

+8
python nice
source share
2 answers

you can always start the process with nice pythonscript ,

but if you want to set a good level in a script, you can do:

 import os os.nice(20) 

You can gradually increase a good level longer than a script, so it uses less and less resources over time, which is a simple matter of integrating it into a script.

Also, from outside the script, after you run it, you should use renice -p <pid>

+16
source share

psutil seems like a cross-platform solution for setting process priority for python.

https://github.com/giampaolo/psutil

Windows Solution: http://code.activestate.com/recipes/496767/

 def setpriority(pid=None,priority=1): """ Set The Priority of a Windows Process. Priority is a value between 0-5 where 2 is normal priority. Default sets the priority of the current python process but can take any valid process ID. """ import win32api,win32process,win32con priorityclasses = [win32process.IDLE_PRIORITY_CLASS, win32process.BELOW_NORMAL_PRIORITY_CLASS, win32process.NORMAL_PRIORITY_CLASS, win32process.ABOVE_NORMAL_PRIORITY_CLASS, win32process.HIGH_PRIORITY_CLASS, win32process.REALTIME_PRIORITY_CLASS] if pid == None: pid = win32api.GetCurrentProcessId() handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid) win32process.SetPriorityClass(handle, priorityclasses[priority]) 
+4
source share

All Articles