0
# 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()

I try to run the code above and get an error, hoping to get your answer: cPickle.PicklingError: Can't pickle : attribute lookup builtin.instancemethod failed

loopbing
  • 35
  • 7

1 Answers1

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()
loopbing
  • 35
  • 7