【发布时间】:2020-06-08 18:59:50
【问题描述】:
以下代码尝试并行运行多个命令,每个命令都有超时。如果超时未完成处理,则将停止(我正在使用终止())。
问题是在终止后(返回码设置为 -ve),communicate() 方法挂起,当强制退出 (Ctrl+C) 时会显示以下错误。
(stdout, stderr) = proc.communicate()
File "python3.7/subprocess.py", line 926, in communicate
stdout = self.stdout.read()
代码
procList = []
for app in appList:
try:
p = subprocess.Popen(app['command'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
procList.append((app, p))
except Exception as e:
print(e)
start = time.time()
while len(procList):
time.sleep(30)
try:
for app, proc in procList:
if (time.time() - start > app['timeoutSec']):
proc.terminate()
if proc.poll() is not None and app['mailSent'] == 0:
(stdout, stderr) = proc.communicate() #Hangs here is the process is terminated
send_results_mail('Execution Completed or Terminated')
app['mailSent'] = 1
except subprocess.SubprocessError as e:
print(e)
procList = [(app, proc) for (app, proc) in procList if app['mailSent'] == 0]
【问题讨论】:
-
我正在使用 Linux。
标签: python subprocess timeout terminate