Constantly print Subprocess output while process is running

后端 未结 13 804
庸人自扰
庸人自扰 2020-11-22 06:51

To launch programs from my Python-scripts, I\'m using the following method:

def execute(command):
    process = subprocess.Popen(command, shell=True, stdout=s         


        
相关标签:
13条回答
  • 2020-11-22 07:38

    In Python >= 3.5 using subprocess.run works for me:

    import subprocess
    
    cmd = 'echo foo; sleep 1; echo foo; sleep 2; echo foo'
    subprocess.run(cmd, shell=True)
    

    (getting the output during execution also works without shell=True) https://docs.python.org/3/library/subprocess.html#subprocess.run

    0 讨论(0)
  • 2020-11-22 07:40

    This works at least in Python3.4

    import subprocess
    
    process = subprocess.Popen(cmd_list, stdout=subprocess.PIPE)
    for line in process.stdout:
        print(line.decode().strip())
    
    0 讨论(0)
  • 2020-11-22 07:40

    In Python 3.6 I used this:

    import subprocess
    
    cmd = "command"
    output = subprocess.call(cmd, shell=True)
    print(process)
    
    0 讨论(0)
  • 2020-11-22 07:43

    There is actually a really simple way to do this when you just want to print the output:

    import subprocess
    import sys
    
    def execute(command):
        subprocess.check_call(command, stdout=sys.stdout, stderr=subprocess.STDOUT)
    

    Here we're simply pointing the subprocess to our own stdout, and using existing succeed or exception api.

    0 讨论(0)
  • 2020-11-22 07:48

    To answer the original question, the best way IMO is just redirecting subprocess stdout directly to your program's stdout (optionally, the same can be done for stderr, as in example below)

    p = Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
    p.communicate()
    
    0 讨论(0)
  • 2020-11-22 07:49

    You can use iter to process lines as soon as the command outputs them: lines = iter(fd.readline, ""). Here's a full example showing a typical use case (thanks to @jfs for helping out):

    from __future__ import print_function # Only Python 2.x
    import subprocess
    
    def execute(cmd):
        popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
        for stdout_line in iter(popen.stdout.readline, ""):
            yield stdout_line 
        popen.stdout.close()
        return_code = popen.wait()
        if return_code:
            raise subprocess.CalledProcessError(return_code, cmd)
    
    # Example
    for path in execute(["locate", "a"]):
        print(path, end="")
    
    0 讨论(0)
提交回复
热议问题