开发者

Capture subprocess output [duplicate]

开发者 https://www.devze.com 2022-12-24 02:16 出处:网络
This question already has answers here: Read streaming input from subprocess.commu开发者_运维百科nicate()
This question already has answers here: Read streaming input from subprocess.commu开发者_运维百科nicate() (7 answers) Closed 6 years ago.

I learned that when executing commands in Python, I should use subprocess. What I'm trying to achieve is to encode a file via ffmpeg and observe the program output until the file is done. Ffmpeg logs the progress to stderr.

If I try something like this:

child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
complete = False
while not complete:
    stderr = child.communicate()

    # Get progress
    print "Progress here later"
    if child.poll() is not None:
        complete = True
    time.sleep(2)

the programm does not continue after calling child.communicate() and waits for the command to complete. Is there any other way to follow the output?


communicate() blocks until the child process returns, so the rest of the lines in your loop will only get executed after the child process has finished running. Reading from stderr will block too, unless you read character by character like so:

import subprocess
import sys
child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE)
while True:
    out = child.stderr.read(1)
    if out == '' and child.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()

This will provide you with real-time output. Taken from Nadia's answer here.


.communicate() "Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate."

Instead, you should be able to just read from child.stderr like an ordinary file.

0

精彩评论

暂无评论...
验证码 换一张
取 消