title: How to clean up after subprocess.Popen?
body: <p>I have a long-running python script with a perl worker subprocess.  Data is sent in and out of the child proc through its stdin and stdout. Periodically, the child must be restarted.  </p>
  
  <p>Unfortunately, after a while of running, it runs out of files ('too many open files').  lsof shows many remaining open pipes.</p>
  
  <p>What's the proper way to clean up after a Popen'd process?  Here's what I'm doing right now:</p>
  
  <pre><code>def start_helper(self):
      # spawn perl helper
      cwd = os.path.dirname(__file__)
      if not cwd:
          cwd = '.'
  
      self.subp = subprocess.Popen(['perl', 'theperlthing.pl'], shell=False, cwd=cwd,
                                   stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                                   bufsize=1, env=perl_env)
  
  def restart_helper(self):
      # clean up
      if self.subp.stdin:
          self.subp.stdin.close()
      if self.subp.stdout:
          self.subp.stdout.close()
      if self.subp.stderr:
          self.subp.stderr.close()
  
      # kill
      try:
          self.subp.kill()
      except OSError:
          # can't kill a dead proc
          pass
      self.subp.wait() # ?
  
      self.start_helper()
  </code></pre>