Getting realtime output using subprocess

前端 未结 18 2799
执笔经年
执笔经年 2020-11-22 10:12

I am trying to write a wrapper script for a command line program (svnadmin verify) that will display a nice progress indicator for the operation. This requires me to be abl

18条回答
  •  不知归路
    2020-11-22 11:15

    You may use an iterator over each byte in the output of the subprocess. This allows inline update (lines ending with '\r' overwrite previous output line) from the subprocess:

    from subprocess import PIPE, Popen
    
    command = ["my_command", "-my_arg"]
    
    # Open pipe to subprocess
    subprocess = Popen(command, stdout=PIPE, stderr=PIPE)
    
    
    # read each byte of subprocess
    while subprocess.poll() is None:
        for c in iter(lambda: subprocess.stdout.read(1) if subprocess.poll() is None else {}, b''):
            c = c.decode('ascii')
            sys.stdout.write(c)
    sys.stdout.flush()
    
    if subprocess.returncode != 0:
        raise Exception("The subprocess did not terminate correctly.")
    

提交回复
热议问题