title: Python multiprocessing - How to create a function that parallelizes a for loop
body: <p>If you open a Jupyter Notebook and run this:</p>
  
  <pre><code>import multiprocessing
  def f(x):
      a = 3 * x
      pool = multiprocessing.Pool(processes=1)
      global g
      def g(j):
          return a * j
      return pool.map(g, range(5))
  f(1)
  </code></pre>
  
  <p>You will get the following errors</p>
  
  <pre><code>Process ForkPoolWorker-1:
  Traceback (most recent call last):
    File "/Users/me/anaconda3/lib/python3.5/multiprocessing/process.py", line 249, in _bootstrap
      self.run()
    File "/Users/me/anaconda3/lib/python3.5/multiprocessing/process.py", line 93, in run
      self._target(*self._args, **self._kwargs)
    File "/Users/me/anaconda3/lib/python3.5/multiprocessing/pool.py", line 108, in worker
      task = get()
    File "/Users/me/anaconda3/lib/python3.5/multiprocessing/queues.py", line 345, in get
      return ForkingPickler.loads(res)
  AttributeError: Can't get attribute 'g' on &lt;module '__main__'&gt;
  </code></pre>
  
  <p>and I'm trying to understand if this is a bug or a feature.</p>
  
  <p>I'm trying to get this working because in my real case <code>f</code> is basically a for loop easily parallelizable (you only change one parameter each iteration) but that takes a lot of time on each iteration! Am I approaching the problem correctly or is there an easier way? (Note: Throughout the notebook f will be called several times with different parameters itself)</p>