title: Redirecting stderr when using multiprocessing.Pool
body: <p>I want to spawn multiple processes using multiprocessing.Pool (python 2.7.13), and redirect stdout / stderr of each process to a file. The problem is it works for stdout, but not for stderr. Here's an example with a single process.</p>
  
  <pre><code>import sys
  import multiprocessing as mp
  
  def foo():
  
      sys.stdout = open('a.out','w')
      sys.stderr = open('a.err', 'w') 
      print("this must go to a.out.")
      raise Exception('this must go to a.err.')
  
      return True
  
  def run():
  
      pool = mp.Pool(4)
      _retvals = []
      _retvals.append( pool.apply_async(foo) )
  
      retvals = [r.get(timeout=10.) for r in _retvals]
  
  if __name__ == '__main__':
      run()
  </code></pre>
  
  <p>Running <code>python stderr.py</code> in terminal (of macbook) produces a.out with correct message ("this must go to a.out"). But it produces empty a.err, and the error message appears in terminal window instead. </p>
  
  <p>If I don't use multiprocessing.Pool and directly run it in the main thread, it produces correct messages on both files. This means replacing run() with the following snippet:</p>
  
  <pre><code>def run():
      foo()
  </code></pre>