当我将multiprocessing.pool.map
应用于list
对象时,list
对象将不受影响:
from multiprocessing import Pool
def identity(x):
return x
num_list = list(range(0, 10))
print("before multiprocessing:")
with Pool(10) as p:
print(p.map(identity, num_list))
print("after multiprocessing:")
print(list(num_list))
打印
before multiprocessing:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
after multiprocessing:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
但是当我在multiprocessing.pool.map
对象上应用map
时,它似乎被擦除了:
from multiprocessing import Pool
def identity(x):
return x
num_list = list(range(0, 10))
num_list = map(identity, num_list)
print("before multiprocessing:")
with Pool(10) as p:
print(p.map(identity, num_list))
print("after multiprocessing:")
print(list(num_list))
打印
before multiprocessing:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
after multiprocessing:
[]
唯一的区别是num_list = map(identity, num_list)
。
num_list
(地图对象)被multiprocessing.pool.map
擦除了吗?
我不确定,但是找不到其他解释。
答案 0 :(得分:0)
map
函数返回一个iterator
,在p.map()
遍历map
obj的最后一个元素后,再次访问map
obj时将不返回任何内容。这是迭代器的功能