与popen python一起使用时,输入命令似乎不起作用 [英] input command doesn't seem to work when used with popen python
问题描述
我正在编写一个执行scala命令的小型python应用程序.用户可以通过STDIN插入命令,然后python应用将其转发到scala解释器.执行命令后,应用程序将显示操作结果.
I am writing a small python application which executes scala commands. A user can insert the command through the STDIN and then the python app forwards them to the scala interpreter. Once the command is executed, the app shows the result of the operation.
这个想法是使用Popen
创建一个管道,通过该管道我可以发送命令并读取结果.这个想法很简单,但是没有用.我不明白的是,为什么打开管道后sys.stdin
不再起作用.这使得无法在python中读取命令.
The idea is to use Popen
to create a pipe by which I can send commands and read results. The idea is quite simple, but it doesn't work. What I don't understand is, why the sys.stdin
doesn't work anymore after the pipe is opened. This makes impossible to read commands in python.
这是我正在使用的代码:
This is the code I am using:
import sys
from subprocess import Popen, PIPE
with Popen(["scala"], stdout=PIPE, stdin=PIPE, bufsize=0, universal_newlines=True) as scala:
while True:
print("Enter scala command >>> ", end="")
sys.stdout.flush()
command = input()
scala.stdin.write(command)
scala.stdin.flush()
print(scala.stdout.readline())
推荐答案
您需要从scala启动时开始读取所有行,然后用新行输入命令,并在之后获得两行输出:
You need to read all the lines from when the scala starts then input the command with a new line and get the two lines of output after:
from subprocess import Popen, PIPE
with Popen(["scala"], stdout=PIPE, stdin=PIPE, bufsize=0, universal_newlines=True) as scala:
for line in scala.stdout:
print(line)
if not line.strip():
break
while True:
command = input("Enter scala command >>> \n")
scala.stdin.write(command+"\n")
scala.stdin.flush()
for line in scala.stdout:
if not line.strip():
break
print(line)
示例运行:
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.
Enter scala command >>> 3+4
scala> 3+4
res0: Int = 7
Enter scala command >>> 4 * 4
scala> 4 * 4
res1: Int = 16
Enter scala command >>> 16 / 4
scala> 16 / 4
res2: Int = 4
要通过使用 unbuffer 的bash运行它来使其工作,似乎可以对输出进行整理问题:
To get it to work from bash running it with unbuffer seems to sort out the output issues:
from subprocess import Popen, PIPE
with Popen(["unbuffer", "-p","scala"], stdout=PIPE, stdin=PIPE, bufsize=0, universal_newlines=True) as scala:
for line in scala.stdout:
print(line)
if not line.strip():
break
while True:
command = input("Enter scala command >>> ")
scala.stdin.write(command+"\n")
scala.stdout.flush()
for line in scala.stdout:
if not line.strip():
break
print(line)
如果您使用的是Mac Os x,则可能应该使用:
If you are using Mac Os x, you should probably use :
with Popen(["script", "-q", "/dev/null", "scala"], stdout=PIPE, stdin=PIPE, bufsize=0, universal_newlines=True) as scala:
来自bash:
print(line)
## -- End pasted text --
Welcome to Scala version 2.11.7 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60).
Type in expressions to have them evaluated.
Type :help for more information.
Enter scala command >>> 4 + 2
scala> 4 + 2
res0: Int = 6
Enter scala command >>> 4 * 12
scala> 4 * 12
res1: Int = 48
Enter scala command >>> 100 // 25
scala> 100 // 25
res2: Int = 100
Enter scala command >>>
有关外壳缓冲区问题的更多信息:
More info regarding shell buffer issues:
- http://www.pixelbeat.org/programming/stdio_buffering/
- > https://unix.stackexchange.com/questions/25372/turn-off -pipe-in-pipeing
- http://www.pixelbeat.org/programming/stdio_buffering/
- https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe
这篇关于与popen python一起使用时,输入命令似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!