title: Multiprocessing: How to use Pool.map on a function defined in a class?
body: <p>When I run something like:</p>
  
  <pre><code>from multiprocessing import Pool
  
  p = Pool(5)
  def f(x):
       return x*x
  
  p.map(f, [1,2,3])
  </code></pre>
  
  <p>it works fine. However, putting this as a function of a class:</p>
  
  <pre><code>class calculate(object):
      def run(self):
          def f(x):
              return x*x
  
          p = Pool()
          return p.map(f, [1,2,3])
  
  cl = calculate()
  print cl.run()
  </code></pre>
  
  <p>Gives me the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>Exception in thread Thread-1:
  Traceback (most recent call last):
    File "/sw/lib/python2.6/threading.py", line 532, in __bootstrap_inner
      self.run()
    File "/sw/lib/python2.6/threading.py", line 484, in run
      self.__target(*self.__args, **self.__kwargs)
    File "/sw/lib/python2.6/multiprocessing/pool.py", line 225, in _handle_tasks
      put(task)
  PicklingError: Can't pickle &lt;type 'function'&gt;: attribute lookup __builtin__.function failed
  </code></pre>
  
  <p>I've seen a post from Alex Martelli dealing with the same kind of problem, but it wasn't explicit enough.</p>