Is it possible to renice a subprocess?

2020-03-01 18:14发布

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?

6条回答
ゆ 、 Hurt°
2楼-- · 2020-03-01 18:22

without proper rights you can renice only in one way

查看更多
【Aperson】
3楼-- · 2020-03-01 18:24

You should use subprocess.Popen instead of os.system, so you can access any results printed to sys.stdout. IIRC, os.system only gives you access to the return value, which is probably '0' and not the nice level.

查看更多
在下西门庆
4楼-- · 2020-03-01 18:27

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
查看更多
成全新的幸福
5楼-- · 2020-03-01 18:36

I created a python script with a CLI in the past. You can find it here: https://github.com/jedie/python-code-snippets/blob/master/CodeSnippets/reniceall.py

查看更多
forever°为你锁心
6楼-- · 2020-03-01 18:40

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.

As an alternative, you could os.nice the parent before you create a child process - which will inherit its parents nice value - and os.nice back again after you've created the child process.

查看更多
7楼-- · 2020-03-01 18:41

Use the preexec_fn parameter of subprocess.Popen:

If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed. (Unix only)

Example:

>>> Popen(["nice"]).communicate()
0
(None, None)
>>> Popen(["nice"], preexec_fn=lambda : os.nice(10)).communicate()
10
(None, None)
>>> Popen(["nice"], preexec_fn=lambda : os.nice(20)).communicate()
19
(None, None)
查看更多
登录 后发表回答