Skip to content

Instantly share code, notes, and snippets.

@EBNull
Created August 21, 2013 18:52
Show Gist options
  • Save EBNull/6298616 to your computer and use it in GitHub Desktop.
Save EBNull/6298616 to your computer and use it in GitHub Desktop.
import subprocess
import signal
def pipeline(cmds, first_stdin=None, last_stdout=None):
"""Pipe together programs using subprocess"""
pcount = len(cmds)
plist = []
for i, cmd in enumerate(cmds):
stdin = None
stdout = None
stderr = None
if i == 0:
#First process
stdin = first_stdin
else:
stdin = plist[i-1].stdout
if i == len(cmds) - 1:
#Last process
stdout = last_stdout
else:
stdout = subprocess.PIPE
#print cmd, stdin, stdout
plist.append(subprocess.Popen(cmd, close_fds=True, stdin=stdin, stdout=stdout, preexec_fn=lambda: signal.signal(signal.SIGPIPE, signal.SIG_DFL)))
ret = plist
#for proc in ret:
# print proc, proc.stdin, proc.stdout
return ret
#-------------------
import os
import sys
def test(argv):
procs = pipeline([['/bin/echo', 'hello\nthere'], ['/bin/grep', 'hel']])[-1].wait()
procs = pipeline([['/bin/echo', 'end\nof\nworld'], ['/bin/grep', 'wor']])[-1].wait()
return 0
if __name__ == '__main__':
sys.exit(test(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment