title: Python multiprocessing: calling pool.map within a function
body: <p>I am trying to use the <code>mutltiprocessing</code> package to use multiple CPUs within a function. When I run a toy example outside of a function it runs in a quarter of a second with no problems (see below).</p>
  
  <pre><code>from multiprocessing import Pool
  import time
  
  start = time.clock()
  
  def f(x):
      return x*x
  
  if __name__ == '__main__':
      with Pool(processes=7) as pool:     
          result = pool.map(f, range(1000))
  
  
  
  print(time.clock() - start)
  </code></pre>
  
  <p>However, when I adapt the same code into a function (see below), it prints <code>True</code> to indicate that <code>__name__ == '__main__'</code>, but then it runs forever and never returns a result. I am running Python 3.3 on Windows 7.</p>
  
  <pre><code>from multiprocessing import Pool
  import time
  
  start = time.clock()
  
  def f(x):
      return x*x
  
  def testfunc(r):
      if __name__ == '__main__':
          print(True)
          with Pool(processes=7) as pool:     
              result = pool.map(f, range(r))
  
      return result
  
  result = testfunc(1000)
  print(time.clock() - start)
  </code></pre>