There's a fork of multiprocessing
called pathos (note: use the version on github) that doesn't need starmap
-- the map functions mirror the API for python's map, thus map can take multiple arguments. With pathos
, you can also generally do multiprocessing in the interpreter, instead of being stuck in the __main__
block. Pathos is due for a release, after some mild updating -- mostly conversion to python 3.x.
Python 2.7.5 (default, Sep 30 2013, 20:15:49)
[GCC 4.2.1 (Apple Inc. build 5566)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(a,b):
... print a,b
...
>>>
>>> from pathos.multiprocessing import ProcessingPool
>>> pool = ProcessingPool(nodes=4)
>>> pool.map(func, [1,2,3], [1,1,1])
1 1
2 1
3 1
[None, None, None]
>>>
>>>
>>> result = pool.map(lambda x: x**2, range(10))
>>> result
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>
>>>
>>> result = pool.amap(pow, [1,2,3], [4,5,6])
>>> result.get()
[1, 32, 729]
>>>
>>>
>>> result = pool.imap(pow, [1,2,3], [4,5,6])
>>> result
<processing.pool.IMapIterator object at 0x110c2ffd0>
>>> list(result)
[1, 32, 729]
pathos
has several ways that that you can get the exact behavior of starmap
.
>>> def add(*x):
... return sum(x)
...
>>> x = [[1,2,3],[4,5,6]]
>>> import pathos
>>> import numpy as np
>>>
>>> pp = pathos.pools.ProcessPool()
>>> pp.map(add, *np.array(x).T)
[6, 15]
>>>
>>> pp.map(lambda x: add(*x), x)
[6, 15]
>>>
>>> _pp = pathos.pools._ProcessPool()
>>> _pp.starmap(add, x)
[6, 15]
>>>
partial
norlambda
do this. I think it has to do with the strange way that functions are passed to the subprocesses (viapickle
). – senderlepool.map(harvester(text,case),case, 1)
by:pool.apply_async(harvester(text,case),case, 1)
– Tung Nguyenreturn
toharvester()
turned @senderie 's response into being inaccurate. That does not help future readers. – Ricalsin