简体   繁体   English

Python不等待MATLAB完成

[英]Python Not Waiting for MATLAB to Finish

I am interfacing a small MATLAB script with Python via the subprocess module. 我通过subprocess模块将一个小的MATLAB脚本与Python连接起来。 As follows: 如下:

cmd='(matlab -nosplash -nodesktop -r "optimizer;quit;")'
p = subprocess.Popen(cmd,stdin=None,stdout=None,shell=True)
#subprocess.Popen.wait(p)
#p.wait()
print "DONE?"

But "DONE" is being printed even before MATLAB starts! 但是即使在MATLAB启动之前,“DONE”也正在打印! My entire code past it is breaking because of this. 我的整个代码都因此而破裂。

I have tried: 我试过了:

  • Using os.system() calls (This is where I started, but I read on SO that its deprecated) 使用os.system()调用(这是我开始的地方,但我在SO上读到它已被弃用)
  • Using p.wait() and subprocess.Popen.wait . 使用p.wait()subprocess.Popen.wait Both don't work. 两者都不起作用。
  • Using a manual pause of 3 minutes (Max. time MATLAB takes to finish on average) Super Sloppy . 使用3分钟的手动暂停(最长时间MATLAB平均完成) Super Sloppy

What am I missing? 我错过了什么?

Works fine for me: 对我来说很好:

import subprocess
retcode = subprocess.call(["matlab", "-nosplash", "-nodesktop", "-r", "quit;"])
print "DONE", retcode

Split the command arguments accordingly, use only options that you actually require (no need for shell=True , for example), use the function that directly does what you are after ( call ), ie, call and wait for completion. 相应地拆分命令参数,仅使用您实际需要的选项(例如,不需要shell=True ),使用直接执行您之后( call )的函数,即调用并等待完成。

Depending on your installation (see http://www.mathworks.com/help/matlab/ref/matlabwindows.html ), Matlab may be launched in a way such that it immediately quits. 根据您的安装(请参阅http://www.mathworks.com/help/matlab/ref/matlabwindows.html),Matlab可能会以立即退出的方式启动。 To handle that, add "-wait" to your argument list. 要处理它,请在参数列表中添加“-wait”。

Start Matlab with the "-wait" flag. 使用“-wait”标志启动Matlab。 From the documenation: 从文件:

"MATLAB is started by a separate starter program which normally launches MATLAB and then immediately quits. Using this option tells the starter program not to quit until MATLAB has terminated. This option is useful when you need to process the results from MATLAB in a script. Calling MATLAB with this option blocks the script from continuing until the results are generated." “MATLAB由一个单独的启动程序启动,通常启动MATLAB然后立即退出。使用此选项告诉启动程序在MATLAB终止之前不要退出。当您需要在脚本中处理MATLAB的结果时,此选项很有用。使用此选项调用MATLAB会阻止脚本继续运行,直到生成结果。“

Based on your response to my comment, let me answer your question with what I did for my application, that had a similar process to yours (albeit in C#). 根据您对我的评论的回复,让我回答您的问题,我为我的申请所做的事情与您的申请有类似的过程(尽管在C#中)。 Instead of trying to force your process to wait for MATLAB to finish up (which is obviously not working right now), just wait for that CSV file to be written to. 而不是试图迫使你的进程等待MATLAB完成(显然现在不能正常工作),只需等待写入该CSV文件即可。 If you're worried about possibly having duplicates, then just append the current date and time to the end of the file, and that should do the trick. 如果您担心可能有重复项,那么只需将当前日期和时间附加到文件的末尾,这应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM