A small `tee` function for splitting stdout/stderr in subprocess, and a `subprocess.Popen` convenience wrapper
Maintained. Last release 12 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and contributor spread.
pip install subprocess-multiteesubprocess-multitee
Multiplex subprocess stdout/stderr to multiple destinations simultaneously.
from subprocess_multitee import Popen, tee, PIPE
import sys
# Capture output AND print to terminal in real-time
proc = Popen(['make'], stdout=tee(PIPE, sys.stdout))
output = proc.stdout.read()
proc.wait()
Installation
pip install subprocess-multitee
Why?
Standard subprocess forces a choice: either capture output (stdout=PIPE) or display it (stdout=sys.stdout). This library lets you do both—plus log to files, discard with DEVNULL, or any combination.
The tee() function creates a pipe that spawns a background thread reading one byte at a time and writing to all destinations, ensuring real-time output even for line-buffered programs.
Usage
Basic: Capture + Display
from subprocess_multitee import Popen, tee, PIPE
import sys
proc = Popen(['python', 'train.py'], stdout=tee(PIPE, sys.stdout))
output = proc.stdout.read() # Full output captured
proc.wait() # Terminal saw it in real-time
Log to File + Display + Capture
with open('build.log', 'wb') as log:
proc = Popen(['make', '-j4'],
stdout=tee(PIPE, sys.stdout, log),
stderr=tee(PIPE, sys.stderr, log))
stdout, stderr = proc.communicate()
With run() Helper
from subprocess_multitee import run, tee, PIPE
import sys
result = run(['pytest', '-v'], stdout=tee(PIPE, sys.stdout), check=True)
print(f"Captured {len(result.stdout)} bytes")
Discard + Capture (silent but recorded)
from subprocess_multitee import Popen, tee, PIPE, DEVNULL
proc = Popen(['noisy-command'], stdout=tee(DEVNULL, PIPE))
output = proc.stdout.read() # Captured, but nothing printed
With stdlib Popen (manual cleanup)
import subprocess
from subprocess_multitee import tee, PIPE
t = tee(PIPE, open('log.txt', 'wb'))
proc = subprocess.Popen(['cmd'], stdout=t)
proc.wait()
t.close() # Required with stdlib Popen
output = t.pipes[0].read()
API
tee(*destinations) -> _Tee
Creates a tee object for use as stdout or stderr in Popen.
can be any combination of: