Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Created September 17, 2013 12:20
Show Gist options
  • Save mgedmin/6593597 to your computer and use it in GitHub Desktop.
Save mgedmin/6593597 to your computer and use it in GitHub Desktop.
pipeline() helper for chaining subprocess.Popen() instances conveniently
import subprocess
def pipeline(*commands):
"""Chain several commands into a pipeline.
E.g. to get the equivalent of
$ zcat foo | grep bar | less
do
pipeline(["zcat", "foo"], ["grep", "bar"], ["less"])
"""
if len(commands) < 2:
raise TypeError('at least two commands needed to form a pipeline')
p = subprocess.Popen(commands[0], stdout=subprocess.PIPE)
for cmd in commands[1:-1]:
p = subprocess.Popen(cmd, stdin=p.stdout, stdout=subprocess.PIPE)
p = subprocess.Popen(commands[-1], stdin=p.stdout)
p.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment