26

I am trying to grab stdout from a subprocess.Popen call and although I am achieving this easily by doing:

cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE) for line in cmd.stdout.readlines(): print line 

I would like to grab stdout in "real time". With the above method, PIPE is waiting to grab all the stdout and then it returns.

So for logging purposes, this doesn't meet my requirements (e.g. "see" what is going on while it happens).

Is there a way to get line by line, stdout while is running? Or is this a limitation of subprocess(having to wait until the PIPE closes).

EDIT If I switch readlines() for readline() I only get the last line of the stdout (not ideal):

In [75]: cmd = Popen('ls -l', shell=True, stdout=PIPE) In [76]: for i in cmd.stdout.readline(): print i ....: t o t a l 1 0 4 
4

8 Answers 8

23

Your interpreter is buffering. Add a call to sys.stdout.flush() after your print statement.

Sign up to request clarification or add additional context in comments.

1 Comment

@alfredodeza: it can't possibly work .readlines() won't return until all output is read (until EOF happens) and therefore adding sys.stdout.flush() won't change anything. It contradicts your requirement: "Is there a way to get line by line, stdout while [the child process] is running?"
20

Actually, the real solution is to directly redirect the stdout of the subprocess to the stdout of your process.

Indeed, with your solution, you can only print stdout, and not stderr, for instance, at the same time.

import sys from subprocess import Popen Popen("./slow_cmd_output.sh", stdout=sys.stdout, stderr=sys.stderr).communicate() 

The communicate() is so to make the call blocking until the end of the subprocess, else it would directly go to the next line and your program might terminate before the subprocess (although the redirection to your stdout will still work, even after your python script has closed, I tested it).

That way, for instance, you are redirecting both stdout and stderr, and in absolute real time.

For instance, in my case I tested with this script slow_cmd_output.sh:

#!/bin/bash for i in 1 2 3 4 5 6; do sleep 5 && echo "${i}th output" && echo "err output num ${i}" >&2; done 

3 Comments

Note: I know the thread is old, but I came across this thread and the answer did not satisfy me. Having found the answer, I thought I might as well post it :)
Are there any pitfalls for this? @Undo
this gave me an UnsupportedOperation error
11

To get output "in real time", subprocess is unsuitable because it can't defeat the other process's buffering strategies. That's the reason I always recommend, whenever such "real time" output grabbing is desired (quite a frequent question on stack overflow!), to use instead pexpect (everywhere but Windows -- on Windows, wexpect).

4 Comments

Expect's line buffering is a topic (recently) dear to my heart; would you mind having a look at my latest question?
@Tobu, sure, had a look and answered (recommending pexpect again).
Also winpexpect module could be used on Windows.
4

As this is a question I searched for an answer to for days, I wanted to leave this here for those who follow. While it is true that subprocess cannot combat the other process's buffering strategy, in the case where you are calling another Python script with subprocess.Popen, you CAN tell it to start an unbuffered python.

command = ["python", "-u", "python_file.py"] p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) for line in iter(p.stdout.readline, ''): line = line.replace('\r', '').replace('\n', '') print line sys.stdout.flush() 

I have also seen cases where the popen arguments bufsize=1 and universal_newlines=True have helped with exposing the hidden stdout.

1 Comment

you could use print line, (note: comma) to avoid stripping newlines (no need replace('\n','') or rstrip(b'\r\n')).
3

Drop the readlines() which is coalescing the output. Also you'll need to enforce line buffering since most commands will interally buffer output to a pipe. For details see: http://www.pixelbeat.org/programming/stdio_buffering/

2 Comments

I went through the link but I am not clear how to enforce buffering in Python, would you be able to clarify?
You enforce buffering on the command. tail -f line buffers by default. For grep, sed etc. you'll need to pass appropriate options to them. Note also the new stdbuf command which can apply line buffering to any command that uses stdio
1
cmd = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE) for line in cmd.stdout: print line.rstrip("\n") 

3 Comments

Roger, this is still waiting for the process to end. The way I can confirm this is happening, is by running a longer process like a system update and passing the information to the log. All lines in a 10 second running process are printed/written to the log at the same time
You're running into a buffer size problem; take my above code and change the command to ["find", "/"] and you will see output before that process ends.
I print(line.rstrip("\n")) and got TypeError: a bytes-like object is required, not 'str' in Python3, do you have some ideas?
0

The call to readlines is waiting for the process to exit. Replace this with a loop around cmd.stdout.readline() (note singular) and all should be well.

3 Comments

ths just returns the last line, not all the lines: for i in cmd.stdout.readline(): print i ....: t o t a l 1 0 4
Yes, my bad. The correct answer is the one by Robert Pate above.
There is answer by Robert Pate (at least search for Robert find nothing). And while your answer is not correct, you should delete it...
0

As stated already the issue is in the stdio library's buffering of printf like statements when no terminal is attached to the process. There is a way around this on the Windows platform anyway. There may be a similar solution on other platforms as well.

On Windows you can force create a new console at process creation. The good thing is this can remain hidden so you never see it (this is done by shell=True inside the subprocess module).

cmd = subprocess.Popen('ls -l', shell=True, stdout=PIPE, creationflags=_winapi.CREATE_NEW_CONSOLE, bufsize=1, universal_newlines=True) for line in cmd.stdout.readlines(): print line 

or

A slightly more complete solution is that you explicitly set the STARTUPINFO params which prevents launching a new and unnecessary cmd.exe shell process which shell=True did above.

class PopenBackground(subprocess.Popen): def __init__(self, *args, **kwargs): si = kwargs.get('startupinfo', subprocess.STARTUPINFO()) si.dwFlags |= _winapi.STARTF_USESHOWWINDOW si.wShowWindow = _winapi.SW_HIDE kwargs['startupinfo'] = si kwargs['creationflags'] = kwargs.get('creationflags', 0) | _winapi.CREATE_NEW_CONSOLE kwargs['bufsize'] = 1 kwargs['universal_newlines'] = True super(PopenBackground, self).__init__(*args, **kwargs) process = PopenBackground(['ls', '-l'], stdout=subprocess.PIPE) for line in cmd.stdout.readlines(): print line 

1 Comment

child's buffering strategy does not matter as long as you use .readlines() that does not return until EOF. Use for line in iter(cmd.stdout.readline, b''): instead. Read my answers to the questions I've linked above

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.