5

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

"""

Indominus
  • 1,228
  • 15
  • 31
  • Why don't you just make your function accept a tuple? – Mad Physicist Jan 23 '19 at 09:46
  • Or use `lambda x: b.boo(*x)`? – Mad Physicist Jan 23 '19 at 09:46
  • hmm, don't most functions accept multiple parameters instead of tuple, kinda ugly to make all functions potentially being paralleled take a tuple. As of the lambda expression solution, I tried, `...pool.map(lambda x: b.boo(*x), ...`, it didn't work, seems python `multipleprocessing` doesn't work on lambda expression at all – Indominus Jan 23 '19 at 10:00
  • How did it not work? Internally, there's no difference between a lambda and a regular function, so I'm having trouble believing that. – Mad Physicist Jan 23 '19 at 10:03
  • trust me, I believe you have trouble believing that as I used to have trouble believing that (or I have simply been doing it wrong). I will update to show what I tried and how it didn't work. – Indominus Jan 23 '19 at 10:49
  • That looks like a scoping error. The authoritative answer fixed it. – Mad Physicist Jan 23 '19 at 16:10

1 Answers1

10

I'm the pathos author. pathos is older than starmap, and doesn't really need it. It solved multiple arguments in a pool exactly the same way that the built-in map does.

>>> 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
... 
>>> b = Bar()
>>> pool = mp.ProcessingPool()
>>> f = lambda x: b.boo(*x)    
>>> results = pool.map(f, [(12, 3, 456), (8, 9, 10), ('a', 'b', 'cde')])
>>> results 
[6, 4, 5]
>>> results = pool.map(b.boo, [12, 9, 'a'], [3, 9, 'b'], [456, 10, 'cde'])
>>> results
[6, 4, 5]
>>> results = map(b.boo, [12, 9, 'a'], [3, 9, 'b'], [456, 10, 'cde'])
>>> list(results)
[6, 4, 5]
>>> 

So, essentially, starmap is unnecessary. However, as it's been recently added to the standard Pool interface in multiprocessing in certain versions of python, it probably should be more prominent in pathos. Note that it already is possible to get to an "augmented" version of starmap from pathos if you like.

>>> import pathos
>>> mp = pathos.helpers.mp
>>> p = mp.Pool()
>>> p.starmap
<bound method Pool.starmap of <multiprocess.pool.Pool object at 0x1038684e0>>
>>> 
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • 1
    It's awesome to get it from the horses mouth like that :) – Mad Physicist Jan 23 '19 at 16:10
  • Thank you for clearing that up! Could you elaborate on this "augmented" `starmap` you mentioned? I must be missing something, but isn't this `p` simply the built-in `multiprocessing.pool.Pool`, why wouldn't I need pathos to construct that? – Indominus Jan 23 '19 at 21:16
  • 1
    Notice it's `multiprocess` not `multiprocessing`... so that indicates the `Pool` uses `dill` and not `pickle` for advanced serialization. – Mike McKerns Jan 23 '19 at 21:59
  • oh..., my bad! Thank you! – Indominus Jan 24 '19 at 01:50