2017-03-17 81 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()) 

謝謝