0

I'm new to python and even newer to parallel code. I am working on a much larger script but I tried to simplify the problem. I am using joblib and copy_reg. When executing my script I don't receive the desired output

p0...p1

but instead I receive this:

('p', <__main__.APR instance at 0x2dc9ea8>)

...

('p', <__main__.APR instance at 0x2dc9bd8>)

I would really appreciate any help!

#!/usr/bin/env python2

from joblib import Parallel, delayed
import copy_reg
import types
import multiprocessing

print'You are running python version', (sys.version)
if float('.'.join(((sys.version).split()[0]).split('.')[0:2]))<2.7:
  print('Error: APR requires Python 2.7')
  sys.exit()

def _pickle_method(method):
    func_name = method.im_func.__name__
    obj = method.im_self
    cls = method.im_class
    return _unpickle_method, (func_name, obj, cls)

def _unpickle_method(func_name, obj, cls):
    for cls in cls.mro():
        try:
            func = cls.__dict__[func_name]
        except KeyError:
            pass
        else:
            break
    return func.__get__(obj, cls)


class APR:
    def __init__(self):
        print('')

    def equilibrate(window, prefix):
        prefix='p' 
        print('p', window)
        return

copy_reg.pickle(types.MethodType, _pickle_method, _unpickle_method)

windows=range(10)

if __name__ == "__main__":

    this=APR()    
    num_cores = multiprocessing.cpu_count()
    results = Parallel(n_jobs=num_cores)(delayed(this.equilibrate)(window) for window in windows)
ta8
  • 313
  • 3
  • 12
  • This error is unrelated to parallelization. You defined `equilibrate` only locally inside `__init__` instead of class scope. – MB-F Mar 09 '17 at 14:55
  • Thank you, I corrected the post. – ta8 Mar 09 '17 at 16:40
  • The first argument of a member function is the class instance `self`, which in you case is assigned to `window`. The correct definition should be `def equilibrate(self, window, prefix)`, or maybe simply `def equilibrate(self, window)` seeing that the `prefix` argument is not used. – MB-F Mar 09 '17 at 16:58

0 Answers0