Python Forum
python function run_pipe_line()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python function run_pipe_line()
#1
file runpipeline.py:
def run_pipe_line(*a):
    from subprocess import PIPE, Popen
    if len(a) == 1 and isinstance(a[0],(list,tuple)):
        # a list of commands instead of one per arg
        a = a[0]
    l = len(a)
    if l < 1: # if no commands then no results
        return []
    if l < 2: # if just one command then just do it
        p = [Popen(a[0])]
    else:
        for n in range(l):
            if n < 1: # if this is first command
                p = [Popen(a[0],stdout=PIPE)]
            elif n == l-1: # if this is last command
                p.append(Popen(a[l-1],stdin=p[n-1].stdout))
            else:
                p.append(Popen(a[n],stdin=p[n-1].stdout,stdout=PIPE))
    r = []
    for n in range(l): # wait for all and collect results
        r.append(p[n].wait())
    return r
a command is a list of strings (arguments, tokens). a pipeline is either a list of commands or a command per function call argument. so you might have commands in cmd1, cmd2, and cmd3 ... you can do either run_pipe_line([cmd1,cmd2,cmd3]) or just run_pipe_line(cmd1,cmd2,cmd3).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run_pipe_line Skaperen 0 1,705 Feb-16-2019, 02:24 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020