【问题标题】:python multiprocessing Can't pickle <type 'instancemethod'> [duplicate]python多处理不能腌制<type'instancemethod'> [重复]
【发布时间】:2018-04-22 04:53:46
【问题描述】:
# 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: Can't pickle : attribute 查找 builtin.instancemethod 失败

【问题讨论】:

    标签: python multiprocessing


    【解决方案1】:
    # 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 2016-01-05
      • 2016-10-22
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多