Home  >  Article  >  Backend Development  >  How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 19:31:03849browse

How to Redirect Child Process Output to Files and Terminal Simultaneously in Python?

How to Output Child Processes' Results to Files and Terminal Simultaneously in Python

When employing subprocess.call(), it's possible to specify file descriptors as outf and errf to redirect stdout and stderr to specific files. However, these results won't be concurrently displayed in the terminal.

Solution Using Popen and Threading:

To overcome this, we can leverage Popen directly and utilize the stdout=PIPE argument to read from the child process's stdout. Here's how:

<code class="python">import subprocess
from threading import Thread

def tee(infile, *files):
    # Forward output from `infile` to `files` in a separate thread
    def fanout(infile, *files):
        for line in iter(infile.readline, ""):
            for f in files:
                f.write(line)

    t = Thread(target=fanout, args=(infile,) + files)
    t.daemon = True
    t.start()
    return t

def teed_call(cmd_args, **kwargs):
    # Override `stdout` and `stderr` arguments with PIPE to capture standard outputs
    stdout, stderr = [kwargs.pop(s, None) for s in ["stdout", "stderr"]]
    p = subprocess.Popen(
        cmd_args,
        stdout=subprocess.PIPE if stdout is not None else None,
        stderr=subprocess.PIPE if stderr is not None else None,
        **kwargs
    )
    
    # Create threads to simultaneously write to files and terminal
    threads = []
    if stdout is not None:
        threads.append(tee(p.stdout, stdout, sys.stdout))
    if stderr is not None:
        threads.append(tee(p.stderr, stderr, sys.stderr))
        
    # Join the threads to ensure IO completion before proceeding
    for t in threads:
        t.join()

    return p.wait()</code>

Using this function, we can execute child processes and write their output to both files and the terminal at the same time:

<code class="python">outf, errf = open("out.txt", "wb"), open("err.txt", "wb")
teed_call(["cat", __file__], stdout=None, stderr=errf)
teed_call(["echo", "abc"], stdout=outf, stderr=errf, bufsize=0)
teed_call(["gcc", "a b"], close_fds=True, stdout=outf, stderr=errf)</code>

The above is the detailed content of How to Redirect Child Process Output to Files and Terminal Simultaneously 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