catching stdout in realtime from subprocess

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newlasdjfk
    New Member
    • Mar 2009
    • 3

    catching stdout in realtime from subprocess

    I have read tons of posts but still can't seem to figure it out.

    I want to subprocess.Pope n() rsync.exe in windows, and print the stdout in python.

    This works...
    Code:
    import subprocess, time, os, sys
    
    cmd = "rsync.exe -vaz souce/ dest/"
    
    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()
    But it doesn't catch the progress until a file is done transfered! I want to print the progress for each file in realtime.

    Using python 3.1 now since I heard it should better to handle IO.
  • newlasdjfk
    New Member
    • Mar 2009
    • 3

    #2
    Well, I found out myslelf.
    I case someone else needs it... here it is. Apparently pythons built in:
    for line in file
    don't consider a linebreak with \r as a true linebreak (at least not in windows). So since I couldn't find out how to get this stdout with universal linebreak, so I made my own loop:

    Code:
            
    seek = 0
    line = ""
    extra_run = 5000 ## rsync.exe send returncode before terminated. Extra bytes.
    while True:
        self.std.seek(seek)
        byte = str(self.std.read(1),"windows-1252")
        if byte == "\n" or byte == "\r":
            print(line)
            line = ""
        else:
            line += byte
        seek += 1
    works like a charm... variables may differ from the above example.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      newlasdjfk,

      Thanks for posting back with your solution.

      BV
      Moderator

      Comment

      Working...