• <small id='9Vg3C'></small><noframes id='9Vg3C'>

    • <bdo id='9Vg3C'></bdo><ul id='9Vg3C'></ul>

      <tfoot id='9Vg3C'></tfoot>

    1. <i id='9Vg3C'><tr id='9Vg3C'><dt id='9Vg3C'><q id='9Vg3C'><span id='9Vg3C'><b id='9Vg3C'><form id='9Vg3C'><ins id='9Vg3C'></ins><ul id='9Vg3C'></ul><sub id='9Vg3C'></sub></form><legend id='9Vg3C'></legend><bdo id='9Vg3C'><pre id='9Vg3C'><center id='9Vg3C'></center></pre></bdo></b><th id='9Vg3C'></th></span></q></dt></tr></i><div id='9Vg3C'><tfoot id='9Vg3C'></tfoot><dl id='9Vg3C'><fieldset id='9Vg3C'></fieldset></dl></div>
    2. <legend id='9Vg3C'><style id='9Vg3C'><dir id='9Vg3C'><q id='9Vg3C'></q></dir></style></legend>
      1. 从子进程中实时捕获标准输出

        catching stdout in realtime from subprocess(从子进程中实时捕获标准输出)

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

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

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

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

                  本文介绍了从子进程中实时捕获标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在 Windows 中 subprocess.Popen() rsync.exe,并在 Python 中打印标准输出.

                  I want to subprocess.Popen() rsync.exe in Windows, and print the stdout in Python.

                  我的代码可以工作,但在文件传输完成之前它无法捕捉进度!我想实时打印每个文件的进度.

                  My code works, but it doesn't catch the progress until a file transfer is done! I want to print the progress for each file in real time.

                  现在使用 Python 3.1,因为我听说它应该更好地处理 IO.

                  Using Python 3.1 now since I heard it should be better at handling IO.

                  import subprocess, time, os, sys
                  
                  cmd = "rsync.exe -vaz -P source/ dest/"
                  p, line = True, 'start'
                  
                  
                  p = subprocess.Popen(cmd,
                                       shell=True,
                                       bufsize=64,
                                       stdin=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       stdout=subprocess.PIPE)
                  
                  for line in p.stdout:
                      print(">>> " + str(line.rstrip()))
                      p.stdout.flush()
                  

                  推荐答案

                  subprocess的一些经验法则.

                  • 从不使用 shell=True.它不必要地调用一个额外的 shell 进程来调用您的程序.
                  • 调用进程时,参数作为列表传递.python 中的 sys.argv 是一个列表,C 中的 argv 也是如此.所以你将 list 传递给 Popen 调用子进程,而不是字符串.
                  • 不阅读时不要将 stderr 重定向到 PIPE.
                  • 当你不写的时候不要重定向 stdin.
                  • Never use shell=True. It needlessly invokes an extra shell process to call your program.
                  • When calling processes, arguments are passed around as lists. sys.argv in python is a list, and so is argv in C. So you pass a list to Popen to call subprocesses, not a string.
                  • Don't redirect stderr to a PIPE when you're not reading it.
                  • Don't redirect stdin when you're not writing to it.

                  例子:

                  import subprocess, time, os, sys
                  cmd = ["rsync.exe", "-vaz", "-P", "source/" ,"dest/"]
                  
                  p = subprocess.Popen(cmd,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.STDOUT)
                  
                  for line in iter(p.stdout.readline, b''):
                      print(">>> " + line.rstrip())
                  

                  也就是说,当 rsync 检测到它连接到管道而不是终端时,它可能会缓冲其输出.这是默认行为 - 当连接到管道时,程序必须显式刷新标准输出以获得实时结果,否则标准 C 库将缓冲.

                  That said, it is probable that rsync buffers its output when it detects that it is connected to a pipe instead of a terminal. This is the default behavior - when connected to a pipe, programs must explicitly flush stdout for realtime results, otherwise standard C library will buffer.

                  要对此进行测试,请尝试运行它:

                  To test for that, try running this instead:

                  cmd = [sys.executable, 'test_out.py']
                  

                  并创建一个包含以下内容的 test_out.py 文件:

                  and create a test_out.py file with the contents:

                  import sys
                  import time
                  print ("Hello")
                  sys.stdout.flush()
                  time.sleep(10)
                  print ("World")
                  

                  执行该子进程应该给您Hello"并等待 10 秒,然后再给World".如果上面的 python 代码而不是 rsync 发生这种情况,这意味着 rsync 本身正在缓冲输出,所以你不走运.

                  Executing that subprocess should give you "Hello" and wait 10 seconds before giving "World". If that happens with the python code above and not with rsync, that means rsync itself is buffering output, so you are out of luck.

                  一种解决方案是直接连接到 pty,使用类似 pexpect 之类的东西.

                  A solution would be to connect direct to a pty, using something like pexpect.

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

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

                  相关文档推荐

                  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 画布滚动条?)
                2. <small id='ZRtV5'></small><noframes id='ZRtV5'>

                      <tbody id='ZRtV5'></tbody>
                  • <legend id='ZRtV5'><style id='ZRtV5'><dir id='ZRtV5'><q id='ZRtV5'></q></dir></style></legend>

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

                            <tfoot id='ZRtV5'></tfoot>