Can I use map / imap / imap_unordered with functions without arguments?
Sometimes I need to use multiprocessing with functions without arguments. I wish I could do something like:
from multiprocessing import Pool
def f(): # no argument
return 1
# TypeError: f() takes no arguments (1 given)
print Pool(2).map(f, range(10))
I could do Process(target=f, args=())
, but I prefer the map
/ imap
/ syntax imap_unordered
. Is there a way to do this?
source to share
map
The first argument function must be a function and must take one argument. This is required because the iterable passed as the second argument will be iterated over, and the values will be passed to the function, one at each iteration.
So your best bet is to override f
to take one argument and ignore it, or write a wrapper function with one argument, ignore the argument and return a return value f
like this
from multiprocessing import Pool
def f(): # no argument
return 1
def throw_away_function(_):
return f()
print(Pool(2).map(throw_away_function, range(10)))
# [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
You cannot use functions lamdba
with pools because they are not matched.
source to share