[英]python communicate() hangs after process terminate()
下面的代碼嘗試並行運行多個命令,每個命令都有一個超時。 如果超時未完成處理,則將停止(我正在使用終止())。
問題是在終止后(返回碼設置為 -ve),communication() 方法掛起,當強制退出 (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]
編輯:這是一個工作示例,使用 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
感謝所有的建議和響應,但沒有什么能解決問題,因為我所跨越的流程正在創建多個級別的子流程。
所以我不得不遞歸地終止進程,為此我使用了使用psutil
的解決方案
詳情請看下面的帖子
https://stackoverflow.com/a/27034438/2393961
一旦所有的子進程和孫進程都被殺死, communicate()
可以正常工作。
我學到的另一件事是,即使htop
可以向您展示進程的樹結構,但每個進程都是獨立的,殺死父進程並不會自動殺死其死者。 感謝我的朋友Noga指出這一點
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.