I got an error when executing code below. The problem seems to be map
doesn't support functions taking multiple inputs, just as in the python built-in multiprocessing
package. But in the built-in package, there is a starmap
that solves this issue. Does pathos.multiprocessing
have the same?
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)
TypeError: boo() missing 2 required positional arguments: 'y' and 'z'
Update for lambda expression as suggested (didn't work):
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)
multiprocess.pool.RemoteTraceback:
"""
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
"""