title: Parallel class function calls using python joblib
body: <p>It is possible to make multiple calls to a function in python using joblib.</p>
  
  <pre><code>from joblib import Parallel, delayed 
  
  def normal(x):
      print "Normal", x
      return x**2
  
  if  __name__ == '__main__':
  
      results = Parallel(n_jobs=2)(delayed(normal)(x) for x in range(20))
      print results
  </code></pre>
  
  <p>Gives: <code>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361]</code></p>
  
  <p>However, what I really want is to call a class function on a list of class instances in parallel. The function simply stores a class variable. Then later I will access this variable. </p>
  
  <pre><code>from joblib import Parallel, delayed 
  
  class A(object):
      def __init__(self, x):
          self.x = x
      def p(self):
          self.y = self.x**2
  
  if  __name__ == '__main__':
  
      runs = [A(x) for x in range(20)]
      Parallel(n_jobs=4)(delayed(run.p() for run in runs))
      for run in runs:
          print run.y
  </code></pre>
  
  <p>This gives an error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):</p>
    
    <p>File "", line 1, in 
        runfile('G:/My Drive/CODE/stackoverflow/parallel_classfunc/parallel_classfunc.py',
    wdir='G:/My Drive/CODE/stackoverflow/parallel_classfunc')</p>
    
    <p>File
    "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py",
    line 710, in runfile
        execfile(filename, namespace)</p>
    
    <p>File
    "C:\ProgramData\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py",
    line 86, in execfile
        exec(compile(scripttext, filename, 'exec'), glob, loc)</p>
    
    <p>File "G:/My
    Drive/CODE/stackoverflow/parallel_classfunc/parallel_classfunc.py",
    line 12, in 
        Parallel(n_jobs=4)(delayed(run.p() for run in runs))</p>
    
    <p>File
    "C:\ProgramData\Anaconda2\lib\site-packages\joblib\parallel.py", line
    183, in delayed
        pickle.dumps(function)</p>
    
    <p>File "C:\ProgramData\Anaconda2\lib\copy_reg.py", line 70, in
    _reduce_ex
        raise TypeError, "can't pickle %s objects" % base.<strong>name</strong></p>
    
    <p>TypeError: can't pickle generator objects</p>
  </blockquote>
  
  <p>How is it possible to use joblib with classes like this? Or is there a better approach? </p>