为什么要在multiprocessing.pool 之前定义pool.map 的函数?

why has the function for pool.map to be defined before multiprocessing.pool?

当使用multiprocessing.pool和pool.map时,似乎必须在创建池之前定义映射函数。为什么这是必要的?

这是工作版本:

import multiprocessing

def my_power(x):
  return x * x

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

获取错误的版本,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)

对于多处理,代码需要序列化。我想运行时在创建池之前序列化所有内容会更容易。否则它必须分析并决定序列化什么。