title: Why does communicate deadlock when used with multiple Popen subprocesses?
body: <p>The following issue does <em>not</em> occur in Python 2.7.3. However, it occurs with both Python 2.7.1 and Python 2.6 on my machine (64-bit Mac OSX 10.7.3). This is code I will eventually distribute, so I would like to know if there is any way to complete this task that does not depend so dramatically on the Python version.</p>
  
  <p>I need to open multiple subprocesses in parallel and write STDIN data to each of them. Normally I would do this using the <code>Popen.communicate</code> method. However, <code>communicate</code> is deadlocking whenever I have multiple processes open at the same time.</p>
  
  <pre><code>import subprocess
  
  cmd = ["grep", "hello"]
  processes = [subprocess.Popen(cmd, stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                                  for _ in range(2)]
  
  for p in processes:
      print p.communicate("hello world\ngoodbye world\n")
  </code></pre>
  
  <p>If I change the number of processes to <code>for _ in range(1)</code>, the output is just as expected:</p>
  
  <pre><code>('hello world\n', '')
  </code></pre>
  
  <p>However, when there are two processes (<code>for _ in range(2)</code>), the process blocks indefinitely. I've tried the alternative of writing to stdin manually:</p>
  
  <pre><code>for p in processes:
      p.stdin.write("hello world\ngoodbye world\n")
  </code></pre>
  
  <p>But then any attempt to read from the processes, (<code>p.stdout.read()</code>, for example) still deadlocks.</p>
  
  <p>At first <a href="http://bugs.python.org/issue13817">this</a> appears to be related, but it specifies that it occurs when multiple threads are being used, and that the deadlocking occurs only very infrequently (while here it always occurs). Is there any way to get this to work on versions of Python before 2.7.3?</p>