我用 psutil.Popen 实例替换了一些 Python subprocess.Popen 实例的使用,并希望行为保持不变。
Popen 状态的 psutil 文档:
A more convenient interface to stdlib subprocess.Popen. It starts a sub process and deals with it exactly as when using subprocess.Popen but in addition it also provides all the methods of psutil.Process class in a single interface.
我发现终止进程时的返回码不一样,如下例所示。
>>> import subprocess
>>> p = subprocess.Popen('sleep 100', shell=True)
>>> p.kill()
>>> p.wait()
-9
>>>
>>> import psutil
>>> p = psutil.Popen('sleep 100', shell=True)
>>> p.kill()
>>> p.wait()
9
>>>
>>> print sys.version
2.7.3 (default, Aug 28 2012, 13:02:46)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)]
>>> print psutil.__version__
2.1.1
当进程终止时,POSIX 对退出代码的保证是什么?以上两个返回码都有效吗?
请您参考如下方法:
POSIX 退出代码的值为 0-255。 POpen 的 retcode 与进程的退出代码并不完全相同,因为 POpen 有时会使用 -ve 代码。 POpen 文档对于何时可以/将会发生非常明确:
引用文档:https://docs.python.org/2/library/subprocess.html#subprocess.Popen.returncode 引用这个问题 How to get exit code when using Python subprocess communicate method?
但是,psutil 文档根本没有提到这一点: https://code.google.com/p/psutil/wiki/Documentation#Classes (请参阅 wait(timeout=None) 部分)。
我想关于哪个“正确”是有争议的,但如果 psutil 应该遵循与 POpen 相同的接口(interface),那么我认为这可能是一个错误/缺失的功能。
