是否可以重新初始化子流程?

2024-09-24 00:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道^{}它非常适合父进程,但我需要对我的子进程执行renice。我找到了这样做的方法,但似乎不太方便,而且太过分了:

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

而且它不会在重新结冰后返回到很好的水平。在

在python中有没有更干净的方法来重新定义子进程?在


Tags: 方法new定义进程os水平pidsystem
3条回答

使用^{}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)

您应该使用subprocess.Popen而不是os.system,这样就可以访问打印到的任何结果系统标准输出. IIRC,os.system只允许您访问返回值,它可能是“0”,而不是nice级别。在

renice通常是由set/getpriority实现的,它似乎还没有进入python操作系统或posix模块(还没有)。所以现在调用renice系统命令似乎是你最好的选择。在

作为一种选择,你可以太好了创建子进程之前的父进程-它将继承其父进程nice值-和太好了在您创建子进程之后再次返回。在

相关问题 更多 >