【问题标题】:Python run external program with input and output streamPython运行带有输入和输出流的外部程序
【发布时间】:2017-08-08 14:27:33
【问题描述】:

我有一个运行输入流的 FORTRAN 程序。所以通常我使用“program.exe 输出”命令来执行程序。但是,我想在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)

这将对输入流进行写入输入。

请帮忙。

【问题讨论】:

    标签: python input stream subprocess


    【解决方案1】:

    抱歉,我发现实际上有很多关于我提出的问题的帖子。

    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())
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2015-10-17
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2014-02-25
      • 1970-01-01
      相关资源
      最近更新 更多