【发布时间】:2018-10-24 17:43:22
【问题描述】:
我有 3 个文件:
sleeper.py
import subprocess
print('start sleeper')
subprocess.run(['sleep', '10'])
print('end sleeper')
waker.py
import subprocess
print('The waker begins')
try:
subprocess.run(['python3', 'sleeper.py'], timeout=5)
except subprocess.TimeoutExpired:
pass
print('The waker ends')
awake.py
import subprocess
print('Awake begin')
try:
subprocess.run(['python3', 'waker.py'], timeout=2.5)
except subprocess.TimeoutExpired:
pass
print('Awake end')
然后我运行python3 awake.py。
但得到以下输出:
Awake begin
The waker begins
start sleeper
Awake end
end sleeper
实际上更准确地说,我立即得到前 3 行打印机,然后 2.5 秒后打印第 4 行并得到我的 bash 提示符,然后 7.5 秒后 end sleeper 出现在我的 bash 提示符上。
如何使通过超时杀死子进程也会杀死该子进程运行的子进程?
【问题讨论】:
-
run应该在超时到期时终止子进程。它也会终止孩子吗?在您的情况下似乎并非如此。一种解决方法是使用Popen,轮询超时,然后杀死进程和子进程,例如:*.com/a/27034438/6451573。这很hacky...
标签: python subprocess