执行以下代码时出现错误。问题似乎是map
不支持接受多个输入的函数,就像在Python内置的multiprocessing
包中一样。但在内置包中,有一个starmap
可以解决这个问题。那么pathos.multiprocessing
有没有类似的功能呢?
import pathos.multiprocessing as mp
class Bar:
def foo(self, name):
return len(str(name))
def boo(self, x, y, z):
sum = self.foo(x)
sum += self.foo(y)
sum += self.foo(z)
return sum
if __name__ == '__main__':
b = Bar()
pool = mp.ProcessingPool()
results = pool.map(b.boo, [(12, 3, 456), (8, 9, 10), ('a', 'b', 'cde')])
print(results)
类型错误:boo()缺少2个必需的位置参数:“y”和“z”
根据建议更新lambda表达式(未成功):
if __name__ == '__main__':
b = Bar()
pool = mp.ProcessingPool()
results = pool.map(lambda x: b.boo(*x), [(12, 3, 456), (8, 9, 10), ('a', 'b', 'cde')])
print(results)
Traceback (most recent call last):
File "C:\Users\yg451\Anaconda3\lib\site-packages\multiprocess\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Users\yg451\Anaconda3\lib\site-packages\multiprocess\pool.py", line 44, in mapstar
return list(map(*args))
File "C:\Users\yg451\Anaconda3\lib\site-packages\pathos\helpers\mp_helper.py", line 15, in
func = lambda args: f(*args)
File "C:/Users/yg451/Code/foo/Machine Learning/xPype/test/scratch.py", line 18, in
results = pool.map(lambda x: b.boo(*x), [(12, 3, 456), (8, 9, 10), ('a', 'b', 'cde')])
NameError: name 'b' is not defined
lambda x: b.boo(*x)
? - Mad Physicist...pool.map(lambda x: b.boo(*x), ...
,但它没有起作用,似乎 Python 的multipleprocessing
根本不支持 lambda 表达式。 - Indominus