2017-03-17 96 views
0

我有一个FORTRAN程序需要输入流才能运行。所以通常我使用“program.exe <输入>输出”命令来执行程序。但是,我想要异步运行python程序。Python运行输入输出流的外部程序

我想:

input = open("Input", 'rb').read() 
running_procs = [Popen(['program.exe'], stdout=PIPE, stdin=PIPE, stderr=PIPE)] 

while running_procs: 
for proc in running_procs: 
    retcode = proc.poll() 
    if retcode is not None: # Process finished. 
     running_procs.remove(proc) 
     break 
else: # No process is done, wait a bit and check again. 
    proc.stdin.write(input) 
    time.sleep(.1) 
    continue 

# Here, `proc` has finished with return code `retcode` 
if retcode != 0: 
    """Error handling.""" 
print(proc.stdout) 

我不知道如果

proc.stdin.write(input) 

这将做文字输入的输入流。

请帮忙。

回答

0

对不起,我发现实际上有很多关于我问的问题。

input = open("Input", 'rb').read() 

running_proc = Popen(['program.exe'], stdout=PIPE, stdin=PIPE, stderr=PIPE) 
out, err = running_proc.communicate(input=input) 
outfile = open("outfile.out", "w") 
outfile.write(out.decode()) 

谢谢