Created
September 17, 2013 12:20
-
-
Save mgedmin/6593597 to your computer and use it in GitHub Desktop.
pipeline() helper for chaining subprocess.Popen() instances conveniently
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
See also https://github.com/ProgrammersOfVilnius/pov-server-page/blob/22177c51e55af393fb06c32b83524ea6000b7b25/update_server_page.py#L146