Python subprocess.call不等待命令执行 [英] Python subprocess.call doesn't wait for command to execute

查看:906
本文介绍了Python subprocess.call不等待命令执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,我需要在课程中使用它进行作业.我在Freemat/octave/matlab .m文件中开发了解决方案(一种优化算法),并希望从Python调用它(python代码将由分级python脚本调用).

I'm new to python, which I need to use for an assignment in a course. I developed the solution (an optimization algorithm) in Freemat / octave / matlab .m file and wanted to call it from Python (the python code will be called by a grading python script).

.m文件读取一个名为tmp.data的文件,并将输出写入output.txt.然后,python脚本应从该输出中读取并将其转换为分级脚本期望的结果.

The .m file reads a file called tmp.data and writes the output to output.txt. The python script should then read from that output and convert it to the result that the grading script expects.

一切运行良好,除了我无法让Python等待对Matlab的调用完成,因此在以下几行上会产生错误.

All runs fine, except I haven't been able to make Python wait for the call to Matlab to complete and therefore generates an error on the following lines.

代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from collections import namedtuple
Item = namedtuple("Item", ['index', 'value', 'weight'])

import subprocess
import os
from subprocess import Popen, PIPE

def solve_it(input_data):
    # Modify this code to run your optimization algorithm

    # Write the inputData to a temporay file
    tmp_file_name = 'tmp.data'
    tmp_file = open(tmp_file_name, 'w')
    tmp_file.write(input_data)
    tmp_file.close()

    # call matlab (or any other solver)
    # subprocess.call('matlab -r gp(\'tmp.data\')', shell=1)
    # run=os.system
    # a=run('matlab -r gp(\'tmp.data\')')
    # process = Popen('matlab -r gp(\'tmp.data\')', stdout=PIPE)
    # Popen.wait()
    # (stdout, stderr) = process.communicate()
    subprocess.call('matlab -r gp(\'tmp.data\')',shell=0)

    # Read result from file
    with open('output.txt') as f:
        result = f.read()

    # remove the temporay file
    os.remove(tmp_file_name)
    os.remove('output.txt')

    return result




    # return stdout.strip()



    # prepare the solution in the specified output format
    # output_data = str(value) + ' ' + str(0) + '\n'
    # output_data += ' '.join(map(str, taken))
    # return output_data


import sys

if __name__ == '__main__':
    if len(sys.argv) > 1:
        file_location = sys.argv[1].strip()
        input_data_file = open(file_location, 'r')
        input_data = ''.join(input_data_file.readlines())
        input_data_file.close()
        print solve_it(input_data)
    else:
        print 'This test requires an input file.  Please select one from the data directory. (i.e. python solver.py ./data/ks_4_0)'

如您所见,我尝试了subprocess.call,popen,os.system ...无济于事.所有这些都给我类似的错误:

As you see, I've tried with subprocess.call, popen, os.system... to no avail. All of them give me similar errors:

C:\Users\gp\Documents\Documents\personal\educacion\Discrete Optimization\knapsack>python2 solver.py data/ks_19_0
Traceback (most recent call last):
  File "solver.py", line 60, in <module>
    print solve_it(input_data)
  File "solver.py", line 30, in solve_it
    with open('output.txt') as f:
IOError: [Errno 2] No such file or directory: 'output.txt'

当然!当 matlab仍在打开过程中时,会出现错误.因此,它正在尝试访问尚未创建的文件.

Of course! The error comes while matlab is still in the process of opening. It thus is trying to access a file that hasn't been created yet.

我该怎么做才能让Python等待Matlab 完成 ??

What should I do to get Python to wait for Matlab to complete??

感谢您的帮助,谢谢.

推荐答案

您的代码似乎忽略了matlab使用启动器(matlab_root/bin/matlab.exe)和主应用程序(matlab_root/bin/bin/xxx/matlab)的事实.可执行程序).要使启动器保持打开状态,直到主应用程序关闭,您必须使用-wait选项.

Your code seems to irgnore the fact that matlab uses a launcher (matlab_root/bin/matlab.exe) and a main application (matlab_root/bin/xxx/matlab.exe). To keep the launcher open until the main application closes, you have to use the -wait option.

这篇关于Python subprocess.call不等待命令执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆