Home > Backend Development > Python Tutorial > How to Efficiently Read and Process Subprocess Output Line by Line in Python?

How to Efficiently Read and Process Subprocess Output Line by Line in Python?

Barbara Streisand
Release: 2024-12-14 16:38:11
Original
824 people have browsed it

How to Efficiently Read and Process Subprocess Output Line by Line in Python?

How to Read Subprocess stdout Line by Line

In Python, using subprocess to call a Linux utility that generates a large volume of output can pose challenges if you want to capture and process the output incrementally.

In the parent process, the for line in proc.stdout statement reads the entire input before iterating over it. This can lead to the output not appearing in your application until a significant amount has been generated.

To address this issue, use proc.stdout.readline() instead:

import subprocess

proc = subprocess.Popen(['python', 'fake_utility.py'], stdout=subprocess.PIPE)
while True:
  line = proc.stdout.readline()
  if not line:
    break
  #the real code does filtering here
  print "test:", line.rstrip()
Copy after login

This solution allows you to filter and print each line of output as it becomes available from the subprocess. Keep in mind that you may still need to consider the subprocess's buffering behavior.

The above is the detailed content of How to Efficiently Read and Process Subprocess Output Line by Line in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template