简体   繁体   English

是否可以修改子流程?

[英]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.我知道os.nice()它非常适合父进程,但我需要对我的子子进程进行 renice。 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?有没有更干净的方法来在 python 中修改子进程?

Use the preexec_fn parameter of subprocess.Popen :使用subprocess.Popenpreexec_fn参数:

If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed.如果preexec_fn设置为可调用对象,则该对象将在子进程执行之前在子进程中调用。 (Unix only) (仅限 Unix)

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)

You should use subprocess.Popen instead of os.system , so you can access any results printed to sys.stdout.您应该使用subprocess.Popen而不是os.system ,这样您就可以访问打印到 sys.stdout 的任何结果。 IIRC, os.system only gives you access to the return value, which is probably '0' and not the nice level. IIRC, os.system只允许您访问返回值,这可能是 '0' 而不是 nice 级别。

renice is usually implemented by set/ getpriority , which doesn't seem to have made it into the python os or posix module(yet?). renice 通常由 set/ getpriority实现,它似乎没有进入 python os 或 posix 模块(还没有?)。 So calling the renice system command seems like your best bet now.所以现在调用 renice 系统命令似乎是你最好的选择。

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.作为替代方案,您可以在创建子进程之前 os.nice 父进程 - 这将继承其父进程的 nice 值 - 并在创建子进程后再次返回 os.nice。

没有适当的权利,你只能以一种方式放弃

renice is usually implemented by set/getpriority , which doesn't seem to have made it into the python os or posix module(yet?). renice 通常由 set/getpriority 实现,它似乎还没有进入 python os 或 posix 模块(还没有?)。 So calling the renice system command seems like your best bet now.所以现在调用 renice 系统命令似乎是你最好的选择。

Expanding Daniel's comment about ctypes :扩展丹尼尔关于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

I created a python script with a CLI in the past.我过去用 CLI 创建了一个 python 脚本。 You can find it here: https://github.com/jedie/python-code-snippets/blob/master/CodeSnippets/reniceall.py你可以在这里找到它: https ://github.com/jedie/python-code-snippets/blob/master/CodeSnippets/reniceall.py

If you want to change the nice of the subprocess without changing the parent, you can start a function which runs the subprocess command with the threading library.如果您想在不更改父进程的情况下更改子进程的 nice,您可以启动一个函数,该函数使用线程库运行子进程命令。

import threading, subprocess

def run():
    os.nice(5)
    subprocess.run(['sha256sum', '/dev/urandom'])

thread = threading.Thread(target=run)
thread.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM