繁体   English   中英

Fortran 未从 subprocess.run 接收输入

[英]Fortran not receiving input from subprocess.run

我正在尝试使用子进程模块从 python 运行 fortran90 代码。 有问题的 fortran 代码提示用户输入,但我希望子进程通过可选参数input提供该输入。 然而,当我这样做时,fortran 运行,提示输入,没有收到任何内容,并且似乎坐在读取语句上。 调用等效的 python 脚本时,相同的代码有效。

我还在使用 capture_output 将 output 写入文件。 我已经确认问题仍然存在,即使已删除。

我已经包含了我想做的简单版本,包括 python 和 fortran 版本。 这是调用 python/fortran 的代码

import os
import subprocess

output1 = os.getcwd() = '/output1'
output2 = os.getcwd() = '/output1'

send = b'input' #b as require bytes type data input

#Call to python code
result = subprocess.run(['python','test.py'],input=send,capture_output=True)

with open(output1,'w') as f:
    f.write(result.stdout)

#Call to fortran code
result = subprocess.run(['./test'],input=send,capture_output=True)

with open(output2,'w') as f:
    f.write(result.stdout)

fortran

program readwrite

  implicit none
  character(len=99) :: readwrite 

  write(*,*) "enter something"
  read(*,*) readwrite
  write(*,*) readwrite

和 python

readwrite = input('enter something')
print(readwrite)

如果有帮助,控制台 output

(base) [a1724542@l01 testing]$ python caller.py
^CTraceback (most recent call last):
  File "caller.py", line 18, in <module>
    result = subprocess.run(['./test'],input=send,capture_output=True)
  File "/apps/skl/software/Anaconda3/2020.07/lib/python3.8/subprocess.py", line 491, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/apps/skl/software/Anaconda3/2020.07/lib/python3.8/subprocess.py", line 1024, in communicate
    stdout, stderr = self._communicate(input, endtime, timeout)
  File "/apps/skl/software/Anaconda3/2020.07/lib/python3.8/subprocess.py", line 1866, in _communicate
    ready = selector.select(timeout)
  File "/apps/skl/software/Anaconda3/2020.07/lib/python3.8/selectors.py", line 415, in select
    fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt

(base) [a1724542@l01 testing]$ cat output1
promptinput
(base) [a1724542@l01 testing]$ cat output2
cat: output2: No such file or directory

我在 python 版本 3.8.3 并使用 ifort/2017.1.132-GCC-5.4.0-2.26 编译器非常感谢

所以我自己偶然发现了答案。 Fortran 将继续读取,直到遇到行尾字符。 因此,修复我上面的示例所需要做的就是将换行符放入输入字符串中,如下所示

send = b'input\n'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM