Python Subprocess:Fortran运行时错误:文件结束

时间:2015-10-09 09:04:40

标签: python fortran subprocess

我试图使用Python子进程在Athena Vortex Lattice中运行一些命令,但它不断抛出错误:

C:\Users\Myself\AppData\Local\Programs\Python\Python35\python.exe C:/Users/Myself/Documents/aerodynamics/analyze_cases.py
Root:  C:\Users\Myself\Documents\aerodynamics
At line 145 of file ../src/userio.f (unit = 5, file = 'stdin')
Fortran runtime error: End of file
Traceback (most recent call last):
  File "C:/Users/Myself/Documents/aerodynamics/analyze_cases.py", line 31, in <module>
    process.communicate(b'\n')
  File "C:\Users\Myself\AppData\Local\Programs\Python\Python35\lib\subprocess.py", line 1043, in communicate
Loaded
    raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication

Process finished with exit code 1

这是使用的代码:

import time
import subprocess
import os

root = os.getcwd()
print("Root: ", root)

# Start AVL Program
process = subprocess.Popen([root+r"/avl.exe "], shell = True, stdin=subprocess.PIPE, bufsize=1,  stdout=subprocess.PIPE)

time.sleep(2)

# Start program with LOAD and filename:    
process.communicate(input=b"LOAD "+root.encode()+b"\input_cases\sample.avl \n")

time.sleep(2)
print("Loaded")

process.communicate(b'\n')
time.sleep(5)

print("Leaving")
# process.communicate(b'\n')
process.communicate(b'\n')
time.sleep(0.5)
process.communicate(b'QUIT')

process.kill()

我的想法:它出现在第一个通信语句中(在Loaded之前)并在尝试将第二个命令发送到现在不存在的进程时崩溃。

我的理论:从日志来看,unit = 5, file = 'stdin'可能会发生一些事情(为什么文件等于stdin?)但我不知道如何解决这个问题。

这里有一些类似的问题,我尝试过以下黑客攻击:

  • shell tr​​ue / false
  • encode()和bitstring stuff
  • 子进程通信而不是stdin.write
  • 同样的问题出现在Mac上的葡萄酒。程序名义上使用相同的命令在Python之外直接命令行。

1 个答案:

答案 0 :(得分:1)

这是一个代码示例,其中修复了代码中的一些问题。您应该考虑是否可以摆脱time.sleep()

#!/usr/bin/env python3
import os
import time
from subprocess import Popen, PIPE, DEVNULL

# start AVL Program
with Popen(os.path.abspath("avl.exe"), stdin=PIPE, stdout=DEVNULL, bufsize=1,
           universal_newlines=True) as process:
    time.sleep(2)
    # start program with LOAD and filename:    
    print("LOAD " + os.path.abspath(r"input_cases\sample.avl"), file=process.stdin)
    time.sleep(2)
    print(file=process.stdin) # send newline
    time.sleep(5)
    print(file=process.stdin) # send newline
    time.sleep(0.5)
    print("QUIT", file=process.stdin)