title: multiprocessing returns "too many open files" but using `with...as` fixes it. Why?
body: <p>I was using <a href="https://stackoverflow.com/a/12097555/316232">this answer</a> in order to run parallel commands with multiprocessing in Python on a Linux box.   </p>
  
  <p>My code did something like:</p>
  
  <pre><code>import multiprocessing
  import logging
  
  def cycle(offset):
      # Do stuff
  
  def run():
      for nprocess in process_per_cycle:
          logger.info("Start cycle with %d processes", nprocess)
          offsets = list(range(nprocess))
          pool = multiprocessing.Pool(nprocess)
          pool.map(cycle, offsets)
  </code></pre>
  
  <p>But I was getting this error: <code>OSError: [Errno 24] Too many open files</code><br>
  So, the code was opening too many file descriptor, i.e.: it was starting too many processes and not terminating them.</p>
  
  <p>I fixed it replacing the last two lines with these lines:</p>
  
  <pre><code>    with multiprocessing.Pool(nprocess) as pool:
          pool.map(cycle, offsets)
  </code></pre>
  
  <p>But I do not know exactly why those lines fixed it.</p>
  
  <p>What is happening underneath of that <code>with</code>?</p>