• <legend id='KzuS2'><style id='KzuS2'><dir id='KzuS2'><q id='KzuS2'></q></dir></style></legend>

      • <bdo id='KzuS2'></bdo><ul id='KzuS2'></ul>

        <small id='KzuS2'></small><noframes id='KzuS2'>

        <i id='KzuS2'><tr id='KzuS2'><dt id='KzuS2'><q id='KzuS2'><span id='KzuS2'><b id='KzuS2'><form id='KzuS2'><ins id='KzuS2'></ins><ul id='KzuS2'></ul><sub id='KzuS2'></sub></form><legend id='KzuS2'></legend><bdo id='KzuS2'><pre id='KzuS2'><center id='KzuS2'></center></pre></bdo></b><th id='KzuS2'></th></span></q></dt></tr></i><div id='KzuS2'><tfoot id='KzuS2'></tfoot><dl id='KzuS2'><fieldset id='KzuS2'></fieldset></dl></div>

      1. <tfoot id='KzuS2'></tfoot>

        与 popen python 一起使用时,输入命令似乎不起作用

        时间:2023-07-21

          <bdo id='P0N3s'></bdo><ul id='P0N3s'></ul>

          <legend id='P0N3s'><style id='P0N3s'><dir id='P0N3s'><q id='P0N3s'></q></dir></style></legend>

            <small id='P0N3s'></small><noframes id='P0N3s'>

            <i id='P0N3s'><tr id='P0N3s'><dt id='P0N3s'><q id='P0N3s'><span id='P0N3s'><b id='P0N3s'><form id='P0N3s'><ins id='P0N3s'></ins><ul id='P0N3s'></ul><sub id='P0N3s'></sub></form><legend id='P0N3s'></legend><bdo id='P0N3s'><pre id='P0N3s'><center id='P0N3s'></center></pre></bdo></b><th id='P0N3s'></th></span></q></dt></tr></i><div id='P0N3s'><tfoot id='P0N3s'></tfoot><dl id='P0N3s'><fieldset id='P0N3s'></fieldset></dl></div>
          • <tfoot id='P0N3s'></tfoot>
              <tbody id='P0N3s'></tbody>

                • 本文介绍了与 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 >>> 
                  ")
                          scala.stdin.write(command+"
                  ")
                          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 似乎整理输出问题:

                  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+"
                  ")
                          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 >>> 
                  

                  有关 shell 缓冲区问题的更多信息:

                  More info regarding shell buffer issues:

                  • http://www.pixelbeat.org/programming/stdio_buffering/李>
                  • https://unix.stackexchange.com/questions/25372/turn-off-管道缓冲

                  这篇关于与 popen python 一起使用时,输入命令似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:子进程标准输入缓冲区未在 bufsize=1 的换行符上刷新 下一篇:Python子进程调用挂起

                  相关文章

                  1. <legend id='GRrnI'><style id='GRrnI'><dir id='GRrnI'><q id='GRrnI'></q></dir></style></legend>
                      <bdo id='GRrnI'></bdo><ul id='GRrnI'></ul>

                    <tfoot id='GRrnI'></tfoot>
                  2. <i id='GRrnI'><tr id='GRrnI'><dt id='GRrnI'><q id='GRrnI'><span id='GRrnI'><b id='GRrnI'><form id='GRrnI'><ins id='GRrnI'></ins><ul id='GRrnI'></ul><sub id='GRrnI'></sub></form><legend id='GRrnI'></legend><bdo id='GRrnI'><pre id='GRrnI'><center id='GRrnI'></center></pre></bdo></b><th id='GRrnI'></th></span></q></dt></tr></i><div id='GRrnI'><tfoot id='GRrnI'></tfoot><dl id='GRrnI'><fieldset id='GRrnI'></fieldset></dl></div>

                    <small id='GRrnI'></small><noframes id='GRrnI'>