与 popen python 一起使用时,输入命令似乎不起作用
input command doesn't seem to work when used with popen python
我正在编写一个小型 python 应用程序来执行 scala 命令。用户可以通过 STDIN 插入命令,然后 python 应用程序将它们转发给 Scala 解释器。执行命令后,应用程序会显示操作结果。
我的想法是使用 Popen
创建一个管道,我可以通过它发送命令和读取结果。这个想法很简单,但是行不通。我不明白的是,为什么 sys.stdin
在管道打开后不再起作用。这使得无法读取 python 中的命令。
这是我正在使用的代码:
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启动时的所有行,然后输入一个新行的命令,得到两行输出:
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
要使其从 bash 运行 与 unbuffer 结合使用似乎可以解决输出问题:
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,您应该使用 :
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 >>>
有关 shell 缓冲区问题的更多信息:
我正在编写一个小型 python 应用程序来执行 scala 命令。用户可以通过 STDIN 插入命令,然后 python 应用程序将它们转发给 Scala 解释器。执行命令后,应用程序会显示操作结果。
我的想法是使用 Popen
创建一个管道,我可以通过它发送命令和读取结果。这个想法很简单,但是行不通。我不明白的是,为什么 sys.stdin
在管道打开后不再起作用。这使得无法读取 python 中的命令。
这是我正在使用的代码:
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启动时的所有行,然后输入一个新行的命令,得到两行输出:
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
要使其从 bash 运行 与 unbuffer 结合使用似乎可以解决输出问题:
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,您应该使用 :
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 >>>
有关 shell 缓冲区问题的更多信息: