Can't pickle using python's multiprocessing Pool.apply_async()

后端 未结 1 1246
半阙折子戏
半阙折子戏 2020-12-13 01:26

I want to run something like this:

from multiprocessing  import Pool
import time
import random

class Controler(object):
    def __init__(self):
        nPr         


        
相关标签:
1条回答
  • 2020-12-13 01:50

    This works, using copy_reg, as suggested by Alex Martelli in the first link you provided:

    import copy_reg
    import types
    import multiprocessing
    
    
    def _pickle_method(m):
        if m.im_self is None:
            return getattr, (m.im_class, m.im_func.func_name)
        else:
            return getattr, (m.im_self, m.im_func.func_name)
    
    copy_reg.pickle(types.MethodType, _pickle_method)
    
    
    class Controler(object):
        def __init__(self):
            nProcess = 10
            pages = 10
            self.__result = []
            self.manageWork(nProcess, pages)
    
        def BarcodeSearcher(self, x):
            return x*x
    
        def resultCollector(self, result):
            self.__result.append(result)
    
        def manageWork(self, nProcess, pages):
            pool = multiprocessing.Pool(processes=nProcess)
            for pag in range(pages):
                pool.apply_async(self.BarcodeSearcher, args=(pag,),
                                 callback=self.resultCollector)
            pool.close()
            pool.join()
    
            print(self.__result)
    
    if __name__ == '__main__':
        Controler()
    
    0 讨论(0)
提交回复
热议问题