摘要
我试图通过子进程调用命令调用外部程序。 当直接从终端调用时,我的程序运行良好。但是,当通过子进程调用时,程序会抛出一个错误。你知道吗
细节
我试图用Python调用程序Zimpl。Zimpl是一个程序,它读取一个模型文件以及两个输入文件并生成一个输出文件。你知道吗
当我通过命令行调用Zimpl时,输出如下:
****************************************************
* Zuse Institute Mathematical Programming Language *
* Release 3.3.2 Copyright (C)2012 by Thorsten Koch *
****************************************************
* This is free software and you are welcome to *
* redistribute it under certain conditions *
* ZIMPL comes with ABSOLUTELY NO WARRANTY *
****************************************************
Reading model.zpl
Reading parfile.par
Reading values.txt
Instructions evaluated: 103351
Name: model.zpl Variables: 156 Constraints: 546 Non Zeros: 2280
Writing [model.lp]
Writing [model.tbl]
当我使用以下命令调用子进程库时:
s = subprocess.check_call(['zimpl', 'model.zpl'])
print(s)
我得到的是:
****************************************************
* Zuse Institute Mathematical Programming Language *
* Release 3.3.2 Copyright (C)2012 by Thorsten Koch *
****************************************************
* This is free software and you are welcome to *
* redistribute it under certain conditions *
* ZIMPL comes with ABSOLUTELY NO WARRANTY *
****************************************************
Reading model.zpl
Reading parfile.par
Reading values.txt
*** Error 800: File model.zpl Line 24 : Symbol "VALUE" not initialised
*** ximize obj : sum <i,j> in Particles : (x[i,j] * VALUE[i,j]);
*** ^^^
Traceback (most recent call last):
File "./runscript.py", line 153, in <module>
evaluateLevel(0, sublevels[0], values)
File "./runscript.py", line 111, in evaluateLevel
s = subprocess.check_call(['zimpl', 'model.zpl'])
File "/Users/macbook/anaconda2/lib/python2.7/subprocess.py", line 541, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['zimpl', 'model.zpl']' returned non-zero exit status 1
在Zimpl模型文件中,初始化值的行如下所示:
param VALUE[Particles] := read "values.txt" as "<1n,2n> 3n";
换句话说,值是从文件中读取的。我甚至查看了Zimpl的源代码来查看错误“Symbol。。出现“未初始化”。我发现:
case SYM_ERR : /* should not happen */
sprintf(errmsg, "Symbol \"%s\" not initialised\n", yytext);
yyerror(errmsg);
我不知道为什么会这样。我猜这可能与缓冲区有关,或者执行程序太快,没有给Zimpl时间读取数据数据.txt文件。你知道吗
更有趣的是。。如果调用Popen命令并打印输出,则程序只部分运行。你知道吗
proc = subprocess.Popen(["zimpl",
"model.zpl"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=0)
proc.wait() # I tried with and without this line, same output
for line in proc.stdout:
print(line),
****************************************************
* Zuse Institute Mathematical Programming Language *
* Release 3.3.2 Copyright (C)2012 by Thorsten Koch *
****************************************************
* This is free software and you are welcome to *
* redistribute it under certain conditions *
* ZIMPL comes with ABSOLUTELY NO WARRANTY *
****************************************************
Reading model.zpl
Reading parfile.par
Reading values.txt
虽然现在它不会抛出错误,但不会像预期的那样生成任何文件。现在。。如果我不尝试通过删除行来打印标准输出:
for line in proc.stdout:
print(line),
瞧!!虽然我看不到程序的输出,但文件是生成的。为什么会这样?我没有提到我尝试过的所有事情,比如使用参数、尝试不同的buffsize、使用子进程库中的其他命令等等。。其他都没用。如果我想看到程序的输出,我不能
最后,如果我运行命令:
proc = subprocess.Popen(["zimpl",
"model.zpl"],
stdin=subprocess.PIPE,
# stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
bufsize=0)
这将正确生成输出和文件,但程序挂起并等待输入:
****************************************************
* Zuse Institute Mathematical Programming Language *
* Release 3.3.2 Copyright (C)2012 by Thorsten Koch *
****************************************************
* This is free software and you are welcome to *
* redistribute it under certain conditions *
* ZIMPL comes with ABSOLUTELY NO WARRANTY *
****************************************************
Reading model.zpl
Reading parfile.par
Reading values.txt
MacBook-Pro:model macbookpro$ Instructions evaluated: 103351
Name: model.zpl Variables: 156 Constraints: 546 Non Zeros: 2280
Writing [model.lp]
Writing [model.tbl]
如果有人能解释一下发生了什么,并提出一种方法来生成输出和文件,并且程序正常终止,而不等待输入,我将非常感激。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐