以有限的优先级启动子进程 [英] Start child process with limited priority

查看:20
本文介绍了以有限的优先级启动子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,如何使用具有低系统优先级的 Python subprocess 模块启动进程,我已经发现:

subprocess 中没有提到priority 手册.

我已经有了似乎有效的解决方案:

self.start_low_priority = ('cmd', '/c', 'start', '/MIN', '/LOW', '/B', '/WAIT')

注意: 开关 /B/WAIT 必须按此顺序才能工作

并将其用作:

args = self.start_low_priority + ('foo.exe', 'bar', 'foobar')subprocess.call( args, shell=False)

但是这个解决方案似乎不是正确和干净的方式加上 进程资源管理器无法从这样启动的应用程序中构建正确的进程树"(因此您无法杀死进程树).>

是否有任何好的做法可以为 Windows 执行此操作?Python 没有为此提供任何我错过的多平台解决方案吗?

解决方案

您可以使用 psutil 库.特别是您可以将优先级设置 psutil.Process.nice 设置为所需的值.

另请参阅答案的示例.

<小时>

编辑:直接查看psutil的文档设置Process.nice已被弃用,你应该使用Process.nice(value) 代替.

I'm looking for a way how to start process with Pythons subprocess module with low system priority, I've already found:

There's no mentioning of priority in subprocess manual.

I already have solution that seems to be working:

self.start_low_priority =  ('cmd', '/c', 'start', '/MIN', '/LOW', '/B', '/WAIT')

NOTE: switches /B /WAIT has to be in this order for this to work

And use it as:

args = self.start_low_priority + ( 'foo.exe', 'bar', 'foobar')
subprocess.call( args, shell=False)

But this solution doesn't seem to be the right and clean way plus Process Explorer is unable to build correct "Process tree" from applications started like this (thus you don't have ability to kill process tree).

Is there any good practice way to do this for windows? Doesn't Python provide any multiplatform solution for this that I've missed?

解决方案

You can use the psutil library. In particular you can set the priority setting psutil.Process.nice to the desired value.

See also this answer for an example.


Edit: Looking at the psutil's documentation setting Process.nice directly is deprecated, you should use Process.nice(value) instead.

这篇关于以有限的优先级启动子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆