Read streaming input from subprocess.communicate()

后端 未结 7 920
攒了一身酷
攒了一身酷 2020-11-21 05:53

I\'m using Python\'s subprocess.communicate() to read stdout from a process that runs for about a minute.

How can I print out each line of that process

7条回答
  •  野的像风
    2020-11-21 06:09

    Adding another python3 solution with a few small changes:

    1. Allows you to catch the exit code of the shell process (I have been unable to get the exit code while using the with construct)
    2. Also pipes stderr out in real time
    import subprocess
    import sys
    def subcall_stream(cmd, fail_on_error=True):
        # Run a shell command, streaming output to STDOUT in real time
        # Expects a list style command, e.g. `["docker", "pull", "ubuntu"]`
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
        for line in p.stdout:
            sys.stdout.write(line)
        p.wait()
        exit_code = p.returncode
        if exit_code != 0 and fail_on_error:
            raise RuntimeError(f"Shell command failed with exit code {exit_code}. Command: `{cmd}`")
        return(exit_code)
    

提交回复
热议问题