Home >Backend Development >Python Tutorial >How Can I Get Live Output from a Subprocess Command in Python?

How Can I Get Live Output from a Subprocess Command in Python?

DDD
DDDOriginal
2024-12-03 08:52:09364browse

How Can I Get Live Output from a Subprocess Command in Python?

Live Output from Subprocess Command

In Python, you can capture the output from a subprocess command and simultaneously produce a live-streaming output using the following approaches:

Using an Iterator

import subprocess
import sys

p = subprocess.Popen(['command'], stdout=subprocess.PIPE)
for line in iter(lambda: p.stdout.readline(1), ''):
    sys.stdout.buffer.write(line)

Using a FileWriter and FileReader

import io
import time
import subprocess
import sys

log = 'test.log'
with io.open(log, 'wb') as writer, io.open(log, 'rb', 1) as reader:
    p = subprocess.Popen(['command'], stdout=writer)
    while p.poll() is None:
        sys.stdout.write(reader.read())
        time.sleep(0.5)
    # Read the remaining
    sys.stdout.write(reader.read())

Original Code Refactoring

In your original code, you can capture the live output after creating a subprocess as follows:

ret_val = subprocess.Popen(run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )
while not ret_val.poll():
    log_file.flush()
    for line in iter(lambda: ret_val.stdout.readline(1), ''):
        if line:
            print(line)
            log_file.write(line.decode())

This method allows you to capture both the stdout and stderr streams and print the live output while simultaneously writing it to a log file.

The above is the detailed content of How Can I Get Live Output from a Subprocess Command in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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