title: Parallel processing with Pool in Python
body: <p>I've attempted to run parallel processing on a locally defined function as follows:</p>
  
  <pre class="lang-py prettyprint-override"><code>import multiprocessing as mp                                                                                               
  import numpy as np
  import pdb
  
  
  def testFunction():                                                                                                        
    x = np.asarray( range(1,10) )
    y = np.asarray( range(1,10) )
  
    def myFunc( i ):
      return np.sum(x[0:i]) * y[i]
  
    p = mp.Pool( mp.cpu_count() )
    out = p.map( myFunc, range(0,x.size) )
    print( out )
  
  
  if __name__ == '__main__':
    print( 'I got here' )                                                                                                         
    testFunction()
  </code></pre>
  
  <p>When I do so, I get the following error:</p>
  
  <pre class="lang-py prettyprint-override"><code>cPickle.PicklingError: Can't pickle &lt;type 'function'&gt;: attribute lookup __builtin__.function failed
  </code></pre>
  
  <p>How can I use multiprocessing to processing several arrays in parallel like I'm trying to do here?  x and y are necessarily defined inside the function; I'd rather not make them global variables.</p>
  
  <p>All help is appreciated.</p>