0
我使用subprocess.Popen
和Popen.communicate
运行一个超时的过程获取输出,类似于给出的建议是这样一个问题:Subprocess.Popen即使在超时的情况下
因此,我现在用的是下面的代码:
with Popen(["strace","/usr/bin/wireshark"], stdout=subprocess.PIPE,stderr=subprocess.PIPE, preexec_fn=os.setsid) as process:
try:
output = process.communicate(timeout=2)[0]
print("Output try", output)
except TimeoutExpired:
os.killpg(process.pid, signal.SIGINT) # send signal to the process group
output = process.communicate()[0]
print("Output except",output)
return output
而且我得到以下输出:
Output except b''
和b''
作为返回值。
即使TimeoutExpired
异常被引发,我怎样才能获得进程的输出(输出直到它被杀死)?
不幸的是,因为这不杀死进程组 - 因此python脚本被阻止。如果我手动杀死进程,输出变量保持为空。 – Sleik