Basically I want to learn how to use the stdout
of one subprocess
(say proc1
) as stdin
of 2 or more other subprocess
A simple way to implement the tee
command in Python is to write to the subprocesses manually:
import gzip
from subprocess import Popen, PIPE
# zcat ABC_C_TPM_26122014.data.gz | tee >(wc -l) >(cksum)
with gzip.open("ABC_C_TPM_26122014.data.gz", "rb") as input_file:
wc = Popen(['wc', '-l'], stdin=PIPE, bufsize=1, close_fds=True)
cksum = Popen(['cksum'], stdin=PIPE, bufsize=1, close_fds=True)
line_count = 0
for line_count, line in enumerate(input_file, start=1):
wc.stdin.write(line)
cksum.stdin.write(line)
wc.stdin.close()
cksum.stdin.close()
wc.wait()
cksum.wait()
print("Line count in the parent: %d" % line_count)
If the lines in the input can be large then you could read the input in chunks: chunk = input_file.read(chunk_size)
instead of line by line (b'\n'
).