Let's suppose that I have an inner function:
def higher(x):
def lower(y):
print(x + y)
return lower
h = higher(10)
I can successfully run this function on a separate process
from multiprocessing import Process
p = Process(target=h, args=(20,))
p.start()
30
But when I try to run it on a pool of processes I get a pickle error:
from multiprocessing import Pool
if __name__ == '__main__':
h = higher(10)
p = Pool(processes=10)
p.map(h, [1, 2, 3])
AttributeError: Can't pickle local object 'higher.<locals>.lower'
I am wondering why there is this difference of behavior between Process and Pool that I thought to be just a set of processes. Honestly I thought that also the version with Process would have raised the same pickle error. (I noticed that the same happens with lambda functions)