Skip to content Skip to sidebar Skip to footer

How To Get The Normal Print Statement Execution When Using Stdout=subprocess.pipe During Subprocess Call In Python

I am starting a subprocess in python through this command: result = subprocess.Popen([sys.executable, '/subscript.py'] + parameter,stdout=subprocess.PIPE ) result.wait() out, err =

Solution 1:

The answer that you've linked prints anything only after the stdout buffer is flushed in the child as it is explicitly said in the answer itself.

Use -u parameter, to unbuffer stdout in the Python child process:

#!/usr/bin/env python2import sys
from subprocess import Popen, PIPE

result = Popen([sys.executable, '-u', '/path/to/subscript.py'] + parameters, 
               stdout=PIPE, bufsize=1)
with result.stdout:
    for line initer(result.stdout.readline, b''):
        print line,
result.wait()

Post a Comment for "How To Get The Normal Print Statement Execution When Using Stdout=subprocess.pipe During Subprocess Call In Python"