通过Python subprocess调用MATLAB脚本出错时如何正常关闭会话?
问题:Python调用MATLAB执行脚本出错时进程卡死无法退出的解决方案
我通过Python的subprocess模块调用MATLAB,使用方式如下,main.py代码示例:
process = subprocess.Popen("bash run.sh", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) stdout = process.communicate()[0]
run.sh内容如下:
export PATH=... /usr/local/MATLAB/R2017b/bin/matlab -r "run('mainscript.m');exit;"
核心业务逻辑在mainscript.m中运行。
当前遇到的问题是:当mainscript.m运行出错时,整个进程会出现卡死现象,MATLAB进程无法正常退出,会话持续留存,导致main.py也无法退出处于挂起状态,但实际需求是main.py遇到该场景时需正常退出或触发告警。
因此想咨询,如何在/usr/local/MATLAB/R2017b/bin/matlab -r "run('mainscript.m');exit;"的调用方式下,实现MATLAB脚本运行出错时自动退出会话?
补充场景细节
当前使用的main.py完整代码:
import subprocess import time from threading import Thread def main(param1): process = subprocess.Popen('bash run.sh', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) start = time.time() stdout = process.communicate()[0] elapsed = time.time() - start print('log: {}, {}, {}'.format(param1, stdout, elapsed)) if __name__=='__main__': _param = {"param1":"param1"} thread = Thread(target=main, kwargs=_param) thread.start()
run.sh内容:
/usr/local/MATLAB/R2017b/bin/matlab -r "run('matlabscript.m');exit;"
matlabscript.m内容如下(其中xxxx为不存在的函数/脚本,用于触发错误):
xxxx;
运行python main.py后,执行ps -u的输出结果:
ubuntu 4901 0.0 0.0 218624 5392 pts/4 Sl+ 08:10 0:00 python main.py ubuntu 4903 0.0 0.0 113280 1192 pts/4 S+ 08:10 0:00 bash run.sh ubuntu 4904 9.0 3.1 5702484 512812 pts/4 Sl+ 08:10 0:06 /usr/local/MATLAB/R2017b/bin/glnxa64/MATLAB -r run('matlabscript.m');exit; -prefersoftwareopengl ubuntu 5025 0.0 0.0 115544 2004 pts/3 Ss 08:11 0:00 -bash ubuntu 5044 0.0 0.0 155436 1852 pts/3 R+ 08:11 0:00 ps -u
此时python main.py完全卡死无法正常退出,手动执行kill 4901杀死主进程后,再次执行ps -u结果如下:
ubuntu 4903 0.0 0.0 113280 1192 pts/4 S 08:10 0:00 bash run.sh ubuntu 4904 4.7 3.1 5702484 512812 pts/4 Sl 08:10 0:06 /usr/local/MATLAB/R2017b/bin/glnxa64/MATLAB -r run('matlabscript.m');exit; -prefersoftwareopengl ubuntu 5025 0.0 0.0 115544 2052 pts/3 Ss 08:11 0:00 -bash ubuntu 5047 0.0 0.0 155436 1852 pts/3 R+ 08:12 0:00 ps -u
可见MATLAB子进程仍然在后台运行,没有正常退出。
可行解决方案
如下修改方案可正常运行:main_with_try_catch.py代码:
import subprocess import time from threading import Thread def main(param1): process = subprocess.Popen('bash run_with_try_catch.sh', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) start = time.time() stdout = process.communicate()[0] elapsed = time.time() - start print('log: {}, {}, {}'.format(param1, stdout, elapsed)) if __name__=='__main__': _param = {"param1":"param1"} thread = Thread(target=main, kwargs=_param) thread.start()
run_with_try_catch.sh内容:
/usr/local/MATLAB/R2017b/bin/matlab -r "try,run('matlabscript.m'),catch,fprintf('error occured'),end;exit;"
运行python main_with_try_catch.py后输出如下:
[ubuntu@localhost ~]$ python main_with_try_catch.py log: param1, MATLAB is selecting SOFTWARE OPENGL rendering. < M A T L A B (R) > Copyright 1984-2017 The MathWorks, Inc. R2017b (9.3.0.713579) 64-bit (glnxa64) September 14, 2017 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. error occured, 7.47159695625 [ubuntu@localhost ~]$
此时MATLAB可正常退出,执行ps -u查看无残留进程,运行符合预期。
内容的提问来源于stack exchange,提问作者mining
相关产品推荐
相关产品推荐

