Why does this implementation of multiprocessing.pool not work?
Objects that you pass between processes when using multiprocessing
must be importable from the __main__
module, so that they can be unpickled in the child. Nested functions, like funct
, are not importable from __main__
, so you get that error. You can achieve what you're trying by using a functools.partial
instead:
from multiprocessing import Poolfrom functools import partialdef funct(arg1, arg2, value): return arg1 * arg2 * valueif __name__ == "__main__": t = [1,2,3,4] arg1 = 4 arg2 = 5 pool = Pool(processes=4) func = partial(funct, arg1, arg2) m4 = pool.map(func,t) print(m4)
Output:
[20, 40, 60, 80]