Python Forum
run_pipe_line - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: run_pipe_line (/thread-16152.html)



run_pipe_line - Skaperen - Feb-16-2019

last year i posted a 22 line version of a function to run a pipeline of multiple commands.

today i am posting a much shorter version with only 10 lines.

pipecmds.py
import subprocess
def pipecmds(*cmds,**opts):
    if not cmds:
        return
    p = []
    for cmd in cmds[:-1]:
        p.append(subprocess.Popen(cmd,stdout=subprocess.PIPE,**opts))
        opts['stdin']=p[-1].stdout
    p.append(subprocess.Popen(cmds[-1],**opts))
    return [x.wait() for x in p]
this function takes a list of the command argument strings in each function call argument for as many commands you want to run in a pipeline up to the maximum resources your hardware, system, or system administrator allows. you may use a tuple in place of any list.

if you saved the old 22 line version, please replace it or augment it with this new 10 line version.