在类中把进程池设为成员变量,同时把self作为参数传给线程池中的函数的时候会报错:
pool objects cannot be passed between processes or pickled
两种方法解决:
第一种
class Test(object):
def run(self):
self.pool=Pool(2)
self.pool.starmap(func, [(xx,yy), (xx, yy)])
def __getstate__(self):
self_dict = self.__dict__.copy()
del self_dict['pool']
return self_dict
def __setstate__(self, state):
self.__dict__.update(state)
第二种:
class Test(object):
def run(self):
with Pool(2) as pool:
pool.starmap(func, [(xx,yy), (xx, yy)])
参考: https://www.ikaze.cn/article/27