title: piping together several subprocesses
body: <p>I have 5 processes <code>p1,p2,...,p5</code> where I want to write some data to stdin of p1, pipe p1 output to p2 stdin and finally read the final result from output of p5.</p>
  
  <p>What I have tried so far:</p>
  
  <pre><code>p1 = Popen(['p1'], stdin=PIPE, stdout=PIPE)
  p2 = Popen(['p2'], stdin=p1.stdout, stdout=PIPE)
  ...
  p5 = Popen(['p5'], stdin=p4.stdout, stdout=PIPE)
  
  # write data to stdin
  p1.stdin.write(indata)
  p1.stdin.close()
  
  # not sure in what order to close the pipes here, if at all
  
  # read output
  out = p5.stdout.read()
  print out
  </code></pre>
  
  <p>The last code snipped just hangs, because I must be doing the read/write operations incorrectly.</p>
  
  <p>I was able to get working single processes using <code>communicate()</code> and two processes without suppling any input to first one (example from Python docs):</p>
  
  <pre><code>output=`dmesg | grep hda`
  ==&gt;
  p1 = Popen(["dmesg"], stdout=PIPE)
  p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
  p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
  output = p2.communicate()[0]
  </code></pre>
  
  <p>But I cannot figure out, how to supply input to first process without hanging the interpreter.</p>
  
  <p>I could also use a bash script for that (that I've already written and works), but I'd like to know, how to achieve same with Python.</p>
  
  <p>So, I'd like to ask, how do do all that properly, specifically in what order to do the read/write/close operations on pipes?</p>
  
  <p>I'm working on 64-bit Linux, if that matters.</p>
  
  <p>EDIT: I forgot to mention that all processes p1,..p5 consume all input they are given, process it, write to stdout and then terminate. Thus, the processes next in pipeline should not terminate before the previous ones have finished processing.</p>
  
  <p>EDIT2: I know that I could also use</p>
  
  <pre><code>command = 'bash -c "p1 | p2 | p3 | p4 | p5"'
  proc = Popen([command], shell=True)
  out, err = proc.communicate(input=indata)
  print out
  </code></pre>
  
  <p>but my main interest is getting know how to chain the pipes purely in python code.</p>