title: Python Subprocess: Too Many Open Files
body: <p>I am using subprocess to call another program and save its return values to a variable. This process is repeated in a loop, and after a few thousands times the program crashed with the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;./extract_pcgls.py&quot;, line 96, in &lt;module&gt;
      SelfE.append( CalSelfEnergy(i) )
    File &quot;./extract_pcgls.py&quot;, line 59, in CalSelfEnergy
      p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    File &quot;/usr/lib/python3.2/subprocess.py&quot;, line 745, in __init__
      restore_signals, start_new_session)
    File &quot;/usr/lib/python3.2/subprocess.py&quot;, line 1166, in _execute_child
      errpipe_read, errpipe_write = _create_pipe()
  OSError: [Errno 24] Too many open files
  </code></pre>
  <p>Code:</p>
  <pre><code>cmd = &quot;enerCHARMM.pl -parram=x,xtop=topology_modified.rtf,xpar=lipid27_modified.par,nobuildall -out vdwaals {0}&quot;.format(cmtup[1])
  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
  out, err = p.communicate()
  </code></pre>