Sometimes I need to use multiprocessing with functions with no 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 syntax of map
/ imap
/ imap_unordered
. Is there a way to do that?
map
function's first argument should be a function and it should accept one argument. It is mandatory because, the iterable passed as the second argument will be iterated and the values will be passed to the function one by one in each iteration.
So, your best bet is to redefine f
to accept one argument and ignore it, or write a wrapper function with one argument, ignore the argument and return the return value of 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 lamdba
functions with pools because they are not picklable.