【问题标题】:Python subprocess.call doesn't wait for command to executePython subprocess.call 不等待命令执行
【发布时间】:2014-03-10 17:42:54
【问题描述】:

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

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

一切运行良好,但我无法让 Python 等待对 Matlab 的调用完成,因此在以下几行中生成错误。

代码如下:

#!/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... 无济于事。他们都给了我类似的错误:

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'

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

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

感谢您的帮助,谢谢。

【问题讨论】:

    标签: python matlab python-2.7 popen


    【解决方案1】:

    [备案]

    正如 Daniel 指出的,通过在 matlab 调用中引入几个选项​​来解决这个问题:

    subprocess.call('matlab -nosplash -wait -r "gp(\'tmp.data\')"',shell=0)

    之后,它运行得很漂亮。

    谢谢

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-27
      • 2018-04-27
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      相关资源
      最近更新 更多