Home >Backend Development >Python Tutorial >How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?

How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-01 00:30:19846browse

How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?

Streaming Output from subprocess.communicate()

In Python, using subprocess.communicate() to read stdout from a process that runs for an extended period can be problematic. The resulting output is received all at once, hindering real-time display.

To overcome this, it's possible to print each line of stdout as it's generated while still ensuring the process termination before proceeding:

Python 2

#!/usr/bin/env python2
from subprocess import Popen, PIPE

p = Popen(["cmd", "arg1"], stdout=PIPE, bufsize=1)
with p.stdout:
    for line in iter(p.stdout.readline, b''):
        print(line)
p.wait()  # Wait for subprocess termination

Python 3

#!/usr/bin/env python3
from subprocess import Popen, PIPE

with Popen(["cmd", "arg1"], stdout=PIPE, bufsize=1, universal_newlines=True) as p:
    for line in p.stdout:
        print(line, end='')

This modified approach continuously reads lines as they are written, bypassing the buffer delay. The process still retains its blocking behavior, halting execution until termination.

The above is the detailed content of How to Stream Subprocess Output in Real-time using Python's `subprocess.communicate()`?. 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