下面的代码尝试并行运行多个命令,每个命令都有一个超时。如果处理在超时之前没有完成,它将被停止(我使用terminate())。
问题是在终止(返回码设置为-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]发布于 2020-02-25 13:43:02
编辑:这是一个使用kill()的工作示例,它有一个行为不端的子进程(不会响应terminate()),但我不确定你的子进程的性质。希望这篇文章能帮助你更好地解决问题!
简单的子程序,Python 3:
import signal
import time
def sighandler(signal, _stack):
print(f"Ignoring signal {signal}")
signal.signal(signal.SIGTERM, sighandler)
signal.signal(signal.SIGINT, sighandler)
secs = 10
print(f"Sleeping {secs} seconds...")
time.sleep(secs)
print("Exiting...")修改的父程序,Python 3:
import subprocess
import time
start = time.time()
appList = [
{'command': 'python ./child.py', 'timeoutSec': 2, 'mailSent': 0},
{'command': 'python ./child.py', 'timeoutSec': 2, 'mailSent': 0},
{'command': 'python ./child.py', 'timeoutSec': 2, 'mailSent': 0},
]
def logmsg(msg):
elap = time.time()-start
print(f"{elap:2.1f} secs: {msg}")
def send_results_mail(result):
logmsg(f"Result: {result}")
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:
logmsg(e)
logmsg(f"Launching child... {p.pid}")
start = time.time()
while len(procList):
time.sleep(1)
try:
for app, proc in procList:
if (time.time() - start > app['timeoutSec']):
logmsg(f"Trying to terminate()...{proc.pid}")
proc.terminate()
proc.kill()
if proc.poll() is not None and app['mailSent'] == 0:
proc.kill()
logmsg(f"Trying to communicate()...{proc.pid}")
(stdout, stderr) = proc.communicate()
send_results_mail('Execution Completed or Terminated')
app['mailSent'] = 1
except subprocess.SubprocessError as e:
logmsg(e)
procList = [(app, proc) for (app, proc) in procList if app['mailSent'] == 0]输出:
$ python parent.py
0.0 secs: Launching child... 537567
0.0 secs: Launching child... 537568
0.0 secs: Launching child... 537569
2.0 secs: Trying to terminate()...537567
2.0 secs: Trying to terminate()...537568
2.0 secs: Trying to terminate()...537569
3.0 secs: Trying to terminate()...537567
3.0 secs: Trying to communicate()...537567
3.0 secs: Result: Execution Completed or Terminated
3.0 secs: Trying to terminate()...537568
3.0 secs: Trying to communicate()...537568
3.0 secs: Result: Execution Completed or Terminated
3.0 secs: Trying to terminate()...537569
3.0 secs: Trying to communicate()...537569
3.0 secs: Result: Execution Completed or Terminated发布于 2020-02-27 23:12:10
感谢所有的建议和回应,但没有什么能解决问题,因为对我来说,我所跨越的流程正在创建多层子流程。
因此,我必须递归地终止进程,为此我使用了使用psutil的解决方案
详情请看下面的帖子
https://stackoverflow.com/a/27034438/2393961
一旦所有子进程和子进程都被终止,communicate()就能正常工作。
我学到的另一点是,即使htop可以向你展示进程的树形结构,但每个进程都是独立的,杀死父进程并不会自动杀死它的后代。感谢我的朋友Noga指出这一点
https://stackoverflow.com/questions/60387918
复制相似问题