python – 是否有可能重新编写子进程?
作者:互联网
我知道os.nice()
它对于父进程来说是完美的,但是我需要对子进程进行管理.我找到了这样做的方法,但似乎不是很方便和过分:
os.system("renice -n %d %d" % ( new_nice, suprocess.pid ) )
并且它在返回后不会返回得到很好的水平.
在python中有更简洁的方法来管理子进程吗?
解决方法:
使用subprocess.Popen
的preexec_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. (Unix only)
例:
>>> 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)
标签:python,subprocess,nice,renice 来源: https://codeday.me/bug/20191006/1861092.html