Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to renice a subprocess?

I know about os.nice() it works perfect for parent process, but I need to do renice of my child subprocesses. I found way to do this, but it seems to be not very handy and too excessive:

os.system("renice -n %d %d" % ( new_nice, suprocess.pid ) )

And it isn't return resulting nice level after renicing.

Is there more clean way to renice subprocesses in python?

like image 957
ramusus Avatar asked Mar 17 '10 15:03

ramusus


People also ask

How do I stop subprocess Popen?

Use subprocess. Popen. Popen(args) with args as a sequence of program arguments or a single string to execute a child program in a new process with the supplied arguments. To terminate the subprocess, call subprocess. Popen. terminate() with subprocess.

Does subprocess Popen block?

Popen is nonblocking. call and check_call are blocking. You can make the Popen instance block by calling its wait or communicate method.

Does Popen wait?

Using Popen Method The Popen method does not wait to complete the process to return a value.

What does subprocess Popen?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.


1 Answers

renice is usually implemented by set/getpriority , which doesn't seem to have made it into the python os or posix module(yet?). So calling the renice system command seems like your best bet now.

Expanding Daniel's comment about ctypes:

from ctypes import cdll
libc = cdll.LoadLibrary("libc.so.6")

for pid in pids:
    print("old priority for PID", pid, "is", libc.getpriority(0, pid))
    libc.setpriority(0, pid, 20)
    print("new priority for PID", pid, "is", libc.getpriority(0, pid))

Result:

old priority for PID 9721 is 0
new priority for PID 9721 is 19
like image 101
Paulo Scardine Avatar answered Sep 28 '22 05:09

Paulo Scardine