Subprocess.popen() should write to log without waiting for process to complete

Here is my test code

            process = sp.Popen(['make'] + [os.path.expandvars(item)
                                           for item in make_line],
                               bufsize=1,
                               universal_newlines=True,
                               stdout=sp.PIPE, stderr=sp.STDOUT,
                               cwd=src_home)
            output, _ = process.communicate()
            inner_logger_console.info(str(output.strip()))

I am trying with bufsize=0 or 1 to get near to real-time output in the logs. What I have to do? process.communicate() has to go right?

What shell are you using to process this script?

I got working solution

with sp.Popen(['make', '-C'] + [os.path.expandvars(item) for item in
                                            make_line],
                          bufsize=0,
                          stdout=sp.PIPE, stderr=sp.STDOUT,
                          cwd=src_home) as process:
                for line in process.stdout:
                    print(line.decode('utf-8').strip())
1 Like