I trying to use the multiprocessing package in python with a Pool.
I have the function f which is called by the map_async function:
from multiprocessing import Pool
def f(host, x):
print host
print x
hosts = ['1.1.1.1', '2.2.2.2']
pool = Pool(processes=5)
pool.map_async(f,hosts,"test")
pool.close()
pool.join()
This code has the next error:
Traceback (most recent call last):
File "pool-test.py", line 9, in <module>
pool.map_async(f,hosts,"test")
File "/usr/lib/python2.7/multiprocessing/pool.py", line 290, in map_async
result = MapResult(self._cache, chunksize, len(iterable), callback)
File "/usr/lib/python2.7/multiprocessing/pool.py", line 557, in __init__
self._number_left = length//chunksize + bool(length % chunksize)
TypeError: unsupported operand type(s) for //: 'int' and 'str'
I don't know how to pass more than 1 argument to the f function. Are there any way?
map_async provides exactly that - an asynchronous parallel map. Create a new python script called asyncmap.py and copy into it.
Use Pool. The multiprocessing pool starmap() function will call the target function with multiple arguments. As such it can be used instead of the map() function. This is probably the preferred approach for executing a target function in the multiprocessing pool that takes multiple arguments.
imap_unordered instead of pool. imap will not have a large effect on the total running time of your code. It might be a little faster, but not by too much. What it may do, however, is make the interval between values being available in your iteration more even.
The “chunksize” is an argument specified in a function to the multiprocessing pool when issuing many tasks.
"test"
is interpreted as map_async
's chunksize
keyword argument (see the docs).
Your code should probably be (here copy-pasted from my IPython session) :
from multiprocessing import Pool
def f(arg):
host, x = arg
print host
print x
hosts = ['1.1.1.1', '2.2.2.2']
args = ((host, "test") for host in hosts)
pool = Pool(processes=5)
pool.map_async(f, args)
pool.close()
pool.join()
## -- End pasted text --
1.1.1.1
test
2.2.2.2
test
Note: In Python 3 you can use starmap
, which will unpack the arguments from the tuples. You'll be able to avoid doing host, x = arg
explicitely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With