read subprocess stdout line by line
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you need live output from a child process, reading stdout line by line is usually better than waiting for the process to finish and collecting everything at once. Python's subprocess module supports this directly, but you need the right combination of text mode, buffering, and process cleanup to avoid confusing behavior.
The Simple Streaming Pattern
For line-based text output, the most straightforward approach is to start the process with Popen, enable text mode, and iterate over process.stdout.
This works well for logs, progress messages, and long-running commands. Using stderr=subprocess.STDOUT also avoids a common deadlock pattern where stdout is drained but stderr fills its pipe and blocks the child.
Why -u or Flushing Matters
Many people get stuck because their loop is correct but the child process buffers its own output. In the example above, python -u tells the child Python process to run unbuffered, which makes lines appear as they are printed.
The same issue exists with non-Python programs. If the child buffers heavily, your parent process cannot read lines that have not been flushed yet. In that case, look for an unbuffered mode, a flush option, or a logging configuration change in the child program.
Binary Mode and Manual Decoding
If you need raw bytes instead of decoded strings, keep text mode off and read with readline.
This pattern is useful when you need explicit encoding control or you are dealing with mixed binary and text data.
When communicate() Is Better
Streaming is not always the right tool. If you only need the final output after the child exits, process.communicate() is simpler and handles both pipes cleanly. Use line-by-line reading when timing matters, not just because it feels lower level.
That distinction is important in tests and short-lived scripts. Sometimes the simplest correct solution is to wait for completion and inspect the full captured output afterwards.
If you also need timeout control, wrap the wait step or the whole operation in your own timeout handling. Streaming output does not remove the need to decide what should happen when a child process stalls forever.
Common Pitfalls
The biggest trap is forgetting that the child may buffer output. When nothing appears, the parent loop is often innocent.
Another mistake is reading stdout line by line while leaving stderr unread in a separate pipe. If the child writes enough stderr data, it can block. Merge the streams or consume both.
Be careful to wait for the process and inspect its return code. Reaching end-of-file on stdout does not automatically mean the command succeeded.
Finally, remember that line iteration works best for text output that is actually line-oriented. If the child prints progress updates without newline characters, you need a different reading strategy.
Summary
- Use
subprocess.Popenwithstdout=PIPEand iterate overprocess.stdoutfor live line-based output. - Enable text mode for normal string processing, or read bytes and decode manually when needed.
- Make sure the child process flushes output, or use an unbuffered mode if available.
- Avoid deadlocks by handling stderr deliberately.
- Use
communicate()instead when you do not need real-time streaming.
Related reading
- Read the value of an attribute of a method
- Reading a file from a private S3 bucket to a pandas dataframe
- Reading a JSON file from S3 using Python boto3
- Reading binary file and looping over each byte
- Reading contents of a gzip file from a AWS S3 in Python
- Reading streaming http response with Python requests library
- Recalling function Tensor 'object' is not callable
- Received a label value of 1 which is outside the valid range of 0, 1 - Python, Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.