Python中的类内的池 [英] Pool within a Class in Python
问题描述
我想在类中使用Pool,但似乎有一个问题。我的代码很长,我创建了一个小演示变种来说明问题。这将是巨大的,如果你可以给我一个变种下面的代码工作。
I would like to use Pool within a class, but there seems to be a problem. My code is long, I created a small-demo variant to illustrated the problem. It would be great if you can give me a variant of the code below that works.
from multiprocessing import Pool
class SeriesInstance(object):
def __init__(self):
self.numbers = [1,2,3]
def F(self, x):
return x * x
def run(self):
p = Pool()
print p.map(self.F, self.numbers)
ins = SeriesInstance()
ins.run()
输出:
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib64/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib64/python2.7/multiprocessing/pool.py", line 319, in _handle_tasks
put(task)
PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed
然后挂起。
推荐答案
看起来像是因为函数传递给worker线程(pickling)你不能使用实例方法不幸的是。我的第一个想法是使用lambdas,但事实证明,内置的 pickler可以'这个解决方案,可悲的是,只是在全局命名空间中使用一个函数。你仍然可以使它成为一个实例属性,看看:
It looks like because of the way the function gets passed to the worker threads (pickling) you can't use instance methods unfortunately. My first thought was to use lambdas, but it turns out the built in pickler can't serialize those either. The solution, sadly, is just to use a function in the global namespace. You can still make it an instance attribute though, take a look:
from multiprocessing import Pool
def F(x):
return x * x
class SeriesInstance(object):
def __init__(self):
self.numbers = [1,2,3]
self.F = F
def run(self):
p = Pool()
out = p.map(self.F, self.numbers)
p.close()
p.join()
return out
if __name__ == '__main__':
print SeriesInstance().run()
这篇关于Python中的类内的池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!