【发布时间】:2021-11-19 11:14:51
【问题描述】:
我目前正在尝试获取以 subprocess.check_call 启动的进程的进程 ID。
即
from subprocess import check_output
# I want to retrieve the PID of this process:
try:
p = check_output(['some broken program'])
except:
if CalledProcessError: # but Popen does not throw me a CalledProcessError even if program crashes
print("triage some stuff")
print(p.pid) # this doesn't work unless its Popen
我尝试过使用 Popen 完美运行,但是,它似乎无法捕获程序终止时,即CalledProcessError。
任何人都可以建议,是否有办法解决这两个问题?谢谢!
【问题讨论】:
-
发布您的代码,以便更清晰
-
对于除
Popen()之外的所有subprocess函数,在被调用进程退出之前,您无法取回控制权,因此不再有 pid。
标签: python subprocess