Home > Backend Development > Python Tutorial > How to Get Real-Time Output from Subprocesses in Python?

How to Get Real-Time Output from Subprocesses in Python?

Mary-Kate Olsen
Release: 2024-10-29 05:49:31
Original
790 people have browsed it

How to Get Real-Time Output from Subprocesses in Python?

Real-Time Output from Subprocesses

In Python, the subprocess module provides a convenient way to execute external programs. However, by default, it captures all the program's output and returns it once the process has completed. This can be problematic for long-running processes.

Introducing Iterative Output Polling

To address this, you can leverage the iter() function to retrieve output line by line as the command generates it. Here's a modified version of your execute() function that implements this approach:

<code class="python">def execute(cmd):
    popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, universal_newlines=True)
    for stdout_line in iter(popen.stdout.readline, ""):
        yield stdout_line
    popen.stdout.close()
    return_code = popen.wait()
    if return_code:
        raise subprocess.CalledProcessError(return_code, cmd)</code>
Copy after login

Usage Example

You can now iterate over the output of the process as it becomes available:

<code class="python">for path in execute(["locate", "a"]):
    print(path, end="")</code>
Copy after login

This example will continuously print the output of the locate a command as it searches for files.

Polling Interval

While this method provides real-time output, the polling interval can impact performance. The universal_newlines parameter ensures that you get lines of text rather than bytes, which incurs an additional overhead. To optimize polling, you can reduce the frequency with which the process's output is checked.

The above is the detailed content of How to Get Real-Time Output from Subprocesses 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