回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我使用Python的<code>subprocess</code>运行MATLAB,如下所示。该<code>main.py</code>如下所示:</p>
<pre><code>process = subprocess.Popen("bash run.sh", stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
stdout = process.communicate()[0]
</code></pre>
<p>而<code>run.sh</code>是这样的:</p>
<pre><code>export PATH=...
/usr/local/MATLAB/R2017b/bin/matlab -r "run('mainscript.m');exit;"
</code></pre>
<p>在{<cd4>}中,主要的事情是在那里完成的</p>
<p>问题是,当运行<code>mainscript.m</code>时出现一些错误,
{<cd6>}似乎被卡住了。并且MATLAB过程不会正常退出。
MATLAB会话似乎仍在运行而没有退出,因此<code>main.py</code>仍在运行而没有退出,这似乎被卡住了。但实际上<code>main.py</code>需要退出或发出一些警报</p>
<p>所以我的问题是,当<code>/usr/local/MATLAB/R2017b/bin/matlab -r "run('mainscript.m');exit;</code>中发生错误时,有没有办法退出MATLAB会话</p>
<p>期待着您的建议。谢谢</p>
<p><strong>更新:</strong></p>
<p>在阅读了善意的评论和回答之后,我提供了更多的细节来澄清这个问题</p>
<p>{<cd2>}是:</p>
<pre><code>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()
</code></pre>
<p>{<cd3>}是:</p>
<pre><code>/usr/local/MATLAB/R2017b/bin/matlab -r "run('matlabscript.m');exit;"
</code></pre>
<p><code>matlabscript.m</code>是(<code>xxxx</code>不是有效的函数/脚本,这会导致错误。):</p>
<pre><code>xxxx;
</code></pre>
<p>运行<code>python main.py</code>时<code>ps -u</code>的输出为:</p>
<pre><code>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
</code></pre>
<p>而且<code>python main.py</code>被</strong>卡在那里,不能正常退出。所以我{<cd17>},然后{<cd15>},它显示:</p>
<pre><code>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
</code></pre>
<p>这意味着<code>MATLAB</code>的子进程仍在运行,没有退出</p>
<p>但是,以下是可以的。
{<cd20>}是:</p>
<pre><code>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()
</code></pre>
<p>{<cd21>}是:</p>
<pre><code>/usr/local/MATLAB/R2017b/bin/matlab -r "try,run('matlabscript.m'),catch,fprintf('error occured'),end;exit;"
</code></pre>
<p>然后,运行<code>python main_with_try_catch.py</code>,它显示:</p>
<pre><code>[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 ~]$
</code></pre>
<p>这意味着MATLAB将正常退出。并检查<code>ps -u</code>,没有剩余进程</p>