Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start child process with limited priority

I'm looking for a way how to start process with Pythons subprocess module with low system priority, I've already found:

  • solution for Unix using preexec_fn and os.nice()
  • resources module which again seems only to be working for Unix
  • some another questions and answers for linux

There's no mentioning of priority in subprocess manual.

I already have solution that seems to be working:

self.start_low_priority =  ('cmd', '/c', 'start', '/MIN', '/LOW', '/B', '/WAIT')

NOTE: switches /B /WAIT has to be in this order for this to work

And use it as:

args = self.start_low_priority + ( 'foo.exe', 'bar', 'foobar')
subprocess.call( args, shell=False)

But this solution doesn't seem to be the right and clean way plus Process Explorer is unable to build correct "Process tree" from applications started like this (thus you don't have ability to kill process tree).

Is there any good practice way to do this for windows? Doesn't Python provide any multiplatform solution for this that I've missed?

like image 663
Vyktor Avatar asked Oct 05 '22 17:10

Vyktor


1 Answers

You can use the psutil library. In particular you can set the priority setting psutil.Process.nice to the desired value.

See also this answer for an example.


Edit: Looking at the psutil's documentation setting Process.nice directly is deprecated, you should use Process.nice(value) instead.

like image 138
Bakuriu Avatar answered Oct 10 '22 04:10

Bakuriu