2

When using multiprocessing.pool and pool.map, it seems that the map function has to be defined before creating the pool. Why is this necessary?

Here is the working version:

import multiprocessing

def my_power(x):
  return x * x

_pool = multiprocessing.Pool()
my_list = _pool.map(my_power, list(range(10)))
print(my_list)

The version that gets the error, AttributeError: Can't get attribute 'my_power'...

_pool = multiprocessing.Pool()

def my_power(x):
  return x * x

my_list = _pool.map(my_power, list(range(10)))
print(my_list)
user7586189
  • 1,249
  • 8
  • 17

1 Answers1

0

For multiprocessing, the code needs to be serialised. I guess it is easier for the runtime to serialise everything before creating the pool. Otherwise it has to analyse and decide what to serialise.

user7586189
  • 1,249
  • 8
  • 17