Home > Backend Development > Python Tutorial > How Can I Simultaneously Capture and Display Live Output from a Subprocess in Python?

How Can I Simultaneously Capture and Display Live Output from a Subprocess in Python?

Susan Sarandon
Release: 2024-12-03 21:58:15
Original
126 people have browsed it

How Can I Simultaneously Capture and Display Live Output from a Subprocess in Python?

Print Live Output While Storing Subprocess Command Output

When invoking a subprocess using subprocess.Popen, we can simultaneously store its output for logging purposes and display it live on the terminal.

Possible Solutions

Option 1: Using Iterators

import subprocess
import sys

with open("test.log", "wb") as f:
    process = subprocess.Popen(your_command, stdout=subprocess.PIPE)
    for c in iter(lambda: process.stdout.read(1), b""):
        sys.stdout.buffer.write(c)
        f.buffer.write(c)
Copy after login

Option 2: Using a Reader and a Writer

import io
import time
import subprocess
import sys

filename = "test.log"
with io.open(filename, "wb") as writer, io.open(filename, "rb", 1) as reader:
    process = subprocess.Popen(command, stdout=writer)
    while process.poll() is None:
        sys.stdout.write(reader.read())
        time.sleep(0.5)
    # Read the remaining
    sys.stdout.write(reader.read())
Copy after login

Option 3: Custom Solution

ret_val = subprocess.Popen(run_command,
                            stdout=log_file,
                            stderr=subprocess.PIPE,
                            shell=True)
while not ret_val.poll():
    log_file.flush()
Copy after login

In another terminal, run:

tail -f log.txt
Copy after login

With these methods, you can capture subprocess output while also displaying it live, ensuring both logging and progress monitoring.

The above is the detailed content of How Can I Simultaneously Capture and Display Live Output from a Subprocess 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