multiprocessing.pool.map会删除地图对象吗?

时间:2019-04-04 11:53:18

标签: python multiprocessing python-multiprocessing

当我将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擦除了吗?

我不确定,但是找不到其他解释。

1 个答案:

答案 0 :(得分:0)

map函数返回一个iterator,在p.map()遍历map obj的最后一个元素后,再次访问map obj时将不返回任何内容。这是迭代器的功能