Python Subprocess: Fortran runtime error: End of file
Python Subprocess: Fortran runtime error: End of file
我正在尝试 运行 使用 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'
可能有问题(为什么文件等于标准输入?)但我不知道如何解决这个问题。
这里有一些类似的问题,我尝试了以下技巧:
- shell true/false
- encode() 和位串东西
- 子进程通信而不是 stdin.write
- Mac 上的 wine 也出现同样的问题。在 Python.
之外直接使用相同的命令对 运行s 进行名义上的编程
这是一个代码示例,您的代码中的一些问题已得到修复。您应该考虑是否也可以摆脱 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)
我正在尝试 运行 使用 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'
可能有问题(为什么文件等于标准输入?)但我不知道如何解决这个问题。
这里有一些类似的问题,我尝试了以下技巧:
- shell true/false
- encode() 和位串东西
- 子进程通信而不是 stdin.write
- Mac 上的 wine 也出现同样的问题。在 Python. 之外直接使用相同的命令对 运行s 进行名义上的编程
这是一个代码示例,您的代码中的一些问题已得到修复。您应该考虑是否也可以摆脱 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)