Getting realtime output using subprocess

前端 未结 18 2613
执笔经年
执笔经年 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:01
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, bufsize=1)
    for line in iter(p.stdout.readline, b''):
        print line,
    p.stdout.close()
    p.wait()
    
    0 讨论(0)
  • 2020-11-22 11:04

    I used this solution to get realtime output on a subprocess. This loop will stop as soon as the process completes leaving out a need for a break statement or possible infinite loop.

    sub_process = subprocess.Popen(my_command, close_fds=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    while sub_process.poll() is None:
        out = sub_process.stdout.read(1)
        sys.stdout.write(out)
        sys.stdout.flush()
    
    0 讨论(0)
  • 2020-11-22 11:06

    Real Time Output Issue resolved: I encountered a similar issue in Python, while capturing the real time output from C program. I added fflush(stdout); in my C code. It worked for me. Here is the code.

    C program:

    #include <stdio.h>
    void main()
    {
        int count = 1;
        while (1)
        {
            printf(" Count  %d\n", count++);
            fflush(stdout);
            sleep(1);
        }
    }
    

    Python program:

    #!/usr/bin/python
    
    import os, sys
    import subprocess
    
    
    procExe = subprocess.Popen(".//count", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
    
    while procExe.poll() is None:
        line = procExe.stdout.readline()
        print("Print:" + line)
    

    Output:

    Print: Count  1
    Print: Count  2
    Print: Count  3
    
    0 讨论(0)
  • 2020-11-22 11:08

    I tried this, and for some reason while the code

    for line in p.stdout:
      ...
    

    buffers aggressively, the variant

    while True:
      line = p.stdout.readline()
      if not line: break
      ...
    

    does not. Apparently this is a known bug: http://bugs.python.org/issue3907 (The issue is now "Closed" as of Aug 29, 2018)

    0 讨论(0)
  • 2020-11-22 11:08

    The Streaming subprocess stdin and stdout with asyncio in Python blog post by Kevin McCarthy shows how to do it with asyncio:

    import asyncio
    from asyncio.subprocess import PIPE
    from asyncio import create_subprocess_exec
    
    
    async def _read_stream(stream, callback):
        while True:
            line = await stream.readline()
            if line:
                callback(line)
            else:
                break
    
    
    async def run(command):
        process = await create_subprocess_exec(
            *command, stdout=PIPE, stderr=PIPE
        )
    
        await asyncio.wait(
            [
                _read_stream(
                    process.stdout,
                    lambda x: print(
                        "STDOUT: {}".format(x.decode("UTF8"))
                    ),
                ),
                _read_stream(
                    process.stderr,
                    lambda x: print(
                        "STDERR: {}".format(x.decode("UTF8"))
                    ),
                ),
            ]
        )
    
        await process.wait()
    
    
    async def main():
        await run("docker build -t my-docker-image:latest .")
    
    
    if __name__ == "__main__":
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())
    
    0 讨论(0)
  • 2020-11-22 11:10

    Few answers suggesting python 3.x or pthon 2.x , Below code will work for both.

     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,)
        stdout = []
        while True:
            line = p.stdout.readline()
            if not isinstance(line, (str)):
                line = line.decode('utf-8')
            stdout.append(line)
            print (line)
            if (line == '' and p.poll() != None):
                break
    
    0 讨论(0)
提交回复
热议问题