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

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

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

        <tfoot id='cgRhd'></tfoot>
      2. 从需要标准输入的子进程实时打印标准输出

        printing stdout in realtime from a subprocess that requires stdin(从需要标准输入的子进程实时打印标准输出)

        <small id='7Vqx9'></small><noframes id='7Vqx9'>

      3. <tfoot id='7Vqx9'></tfoot>

          • <bdo id='7Vqx9'></bdo><ul id='7Vqx9'></ul>
                    <tbody id='7Vqx9'></tbody>
                  <legend id='7Vqx9'><style id='7Vqx9'><dir id='7Vqx9'><q id='7Vqx9'></q></dir></style></legend>

                • <i id='7Vqx9'><tr id='7Vqx9'><dt id='7Vqx9'><q id='7Vqx9'><span id='7Vqx9'><b id='7Vqx9'><form id='7Vqx9'><ins id='7Vqx9'></ins><ul id='7Vqx9'></ul><sub id='7Vqx9'></sub></form><legend id='7Vqx9'></legend><bdo id='7Vqx9'><pre id='7Vqx9'><center id='7Vqx9'></center></pre></bdo></b><th id='7Vqx9'></th></span></q></dt></tr></i><div id='7Vqx9'><tfoot id='7Vqx9'></tfoot><dl id='7Vqx9'><fieldset id='7Vqx9'></fieldset></dl></div>
                  本文介绍了从需要标准输入的子进程实时打印标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是这个问题的后续行动,但如果我愿意要将参数传递给 stdinsubprocess,我怎样才能实时获得输出?这是我目前拥有的;我还尝试用 subprocess 模块中的 call 替换 Popen,这只会导致脚本挂起.

                  This is a follow up to this question, but if I want to pass an argument to stdin to subprocess, how can I get the output in real time? This is what I currently have; I also tried replacing Popen with call from the subprocess module and this just leads to the script hanging.

                  from subprocess import Popen, PIPE, STDOUT
                  cmd = 'rsync --rsh=ssh -rv --files-from=- thisdir/ servername:folder/'
                  p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
                  subfolders = '
                  '.join(['subfolder1','subfolder2'])
                  output = p.communicate(input=subfolders)[0]
                  print output
                  

                  在我不必通过 stdin 的前一个问题中,我被建议使用 p.stdout.readline,那里没有空间可以传输任何内容标准输入.

                  In the former question where I did not have to pass stdin I was suggested to use p.stdout.readline, there there is no room there to pipe anything to stdin.

                  附录:这适用于转移,但我只在最后看到输出,我想在转移发生时查看转移的详细信息.

                  Addendum: This works for the transfer, but I see the output only at the end and I would like to see the details of the transfer while it's happening.

                  推荐答案

                  为了实时从子进程中获取标准输出,你需要准确地决定你想要什么行为;具体来说,您需要决定是要逐行处理输出还是逐字符处理,以及是要在等待输出时阻塞还是在等待时能够做其他事情.

                  In order to grab stdout from the subprocess in real time you need to decide exactly what behavior you want; specifically, you need to decide whether you want to deal with the output line-by-line or character-by-character, and whether you want to block while waiting for output or be able to do something else while waiting.

                  看起来它可能足以让您的情况以行缓冲方式读取输出,阻塞直到每个完整的行进入,这意味着 subprocess 提供的便利功能已经足够好:

                  It looks like it will probably suffice for your case to read the output in line-buffered fashion, blocking until each complete line comes in, which means the convenience functions provided by subprocess are good enough:

                  p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE)
                  # Grab stdout line by line as it becomes available.  This will loop until 
                  # p terminates.
                  while p.poll() is None:
                      l = p.stdout.readline() # This blocks until it receives a newline.
                      print l
                  # When the subprocess terminates there might be unconsumed output 
                  # that still needs to be processed.
                  print p.stdout.read()
                  

                  如果您需要写入进程的标准输入,只需使用另一个管道:

                  If you need to write to the stdin of the process, just use another pipe:

                  p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
                  # Send input to p.
                  p.stdin.write("some input
                  ")
                  p.stdin.flush()
                  # Now start grabbing output.
                  while p.poll() is None:
                      l = p.stdout.readline()
                      print l
                  print p.stdout.read()
                  

                  Pace 另一个答案,不需要间接通过文件来将输入传递给子进程.

                  Pace the other answer, there's no need to indirect through a file in order to pass input to the subprocess.

                  这篇关于从需要标准输入的子进程实时打印标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                • <small id='5ejqH'></small><noframes id='5ejqH'>

                  <tfoot id='5ejqH'></tfoot>

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

                              <tbody id='5ejqH'></tbody>