我正在使用subprocess.Popen
從python運行一些C++二進制應用程序。我該如何處理這個過程的中止?例如,我的C++與斷言打破了,我看到控制檯:檢測進程中止
binary: /usr/include/.../file.hpp:42: Assertion `min <= max' failed.
Aborted
進程關閉,但如何抓住這個使用POPEN處理蟒蛇?
我正在使用subprocess.Popen
從python運行一些C++二進制應用程序。我該如何處理這個過程的中止?例如,我的C++與斷言打破了,我看到控制檯:檢測進程中止
binary: /usr/include/.../file.hpp:42: Assertion `min <= max' failed.
Aborted
進程關閉,但如何抓住這個使用POPEN處理蟒蛇?
如果已經稱爲handle = subprocess.Popen(...)
,你將不得不調用,在某些時候,handle.wait()
. Its return value,這是在同一時間進程句柄對象的returncode
屬性,示出了該過程是否已正常結束(具有值> = 0)或者由於信號而死亡(值爲< 0)。
例子:
>>> import subprocess
>>> subprocess.call("kill -ABRT $$", shell=True)
-6
>>> a = subprocess.Popen("kill -ABRT $$", shell=True)
>>> a.wait()
-6
>>> subprocess.call("kill -SEGV $$", shell=True)
-11
通過C程序看起來像
#include <assert.h>
int main() {
assert(0);
}
我可以做
>>> import subprocess
>>> subprocess.call(["./ass"])
ass: ass.c:4: main: Assertion `0' failed.
-6
所以我也有同樣的效果。
您是否想要檢測進程的結束或在控制檯中捕獲輸出? – luc
@luc檢測進程結束。 'handle.wait()'不能幫助我。 – Ockonal