Redirect stdout of a subprocess to stdin of 2 or more subprocesses

前端 未结 1 411
你的背包
你的背包 2021-01-25 14:50

Basically I want to learn how to use the stdout of one subprocess (say proc1) as stdin of 2 or more other subprocess

相关标签:
1条回答
  • 2021-01-25 15:31

    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').

    0 讨论(0)
提交回复
热议问题