python multiprocessing不能pickle <type'instancemethod'=“”>

时间:2017-11-09 09:05:35

标签: python multiprocessing

# coding: utf-8

import multiprocessing


class A(object):

    def __init__(self):
        pass

    def run(self):
        sheets = ['a', 'b', 'c', 'd', 'e']
        result = []

        pool = multiprocessing.Pool(processes=5)

        for index, sheet in enumerate(sheets):
            result.append(pool.apply_async(self.test, (sheet)))

        pool.close()
        pool.join()

        for data in result:
            print data.get()

    def test(self, args):
        return args


if __name__ == '__main__':
     A().run()

我尝试运行上面的代码并得到一个错误,希望得到你的答案:cPickle.PicklingError:不能pickle:属性     查找内置 .instancemethod失败

1 个答案:

答案 0 :(得分:0)

# coding: utf-8

import multiprocessing

def proxy(cls_instance, args):
    return cls_instance.test(args)


class A(object):

    def __init__(self):
        pass

    def run(self):
        sheets = ['a', 'b', 'c', 'd', 'e']
        result = []

        pool = multiprocessing.Pool(processes=5)

        for index, sheet in enumerate(sheets):
            result.append(pool.apply_async(proxy, (self, sheet)))

        pool.close()
        pool.join()

        for data in result:
            print data.get()

   def test(self, args):
       return args


if __name__ == '__main__':
     A().run()