为什么压缩函数和可迭代的结果不起作用?



所以我有以下代码可以正常工作:

from  concurrent.futures import ProcessPoolExecutor 
import itertools
def grid_search_helper(vec_input):
v1 = vec_input[0]
v2 = vec_input[1]
v3 = vec_input[2]
d = {'v1' : v1, 'v2' : v2, 'v3' : v3}
return(d)
idx = range(0,10)
cutoff = np.ndarray.tolist(np.arange(0.6,0.95,0.05))
opt = [2]
iters = itertools.product(idx, cutoff, opt)
with ProcessPoolExecutor(max_workers = 11) as executor:
for  res in executor.map(grid_search_helper,iters):
print(res)

然后我尝试zip((来打印ProcessPoolExecuter正在处理的可迭代对象,但是当我运行以下代码时,没有打印任何内容:

from  concurrent.futures import ProcessPoolExecutor 
import itertools
def grid_search_helper(vec_input):
v1 = vec_input[0]
v2 = vec_input[1]
v3 = vec_input[2]
d = {'v1' : v1, 'v2' : v2, 'v3' : v3}
return(d)
idx = range(0,10)
cutoff = np.ndarray.tolist(np.arange(0.6,0.95,0.05))
opt = [2]
iters = itertools.product(idx, cutoff, opt)
with ProcessPoolExecutor(max_workers = 11) as executor:
for  res, itr in zip(executor.map(grid_search_helper,iters), iters):
print(res, itr)

我不知道为什么。有人可以帮忙吗?

它与压缩函数和迭代器的事实无关。

问题是您两次使用相同的迭代器:

#                                                      v       v
for res, itr in zip(executor.map(grid_search_helper, iters), iters):
...

第一次传递给map时,它被消耗掉了。当它再次传递给zip时,它已经是空的,所以zip返回一个空的生成器,并且没有任何东西可以迭代。

使用itertools.tee创建同一迭代器的两个副本。

it1, it2 = itertools.tee(itertools.product(idx, cutoff, opt))
with ProcessPoolExecutor(max_workers = 11) as executor:
for  res, itr in zip(executor.map(grid_search_helper,it1), it2):
print(res, itr)

当它尝试执行zip(..., iters)时,先前填充的iters已经为空executor.map(grid_search_helper, iters)因为它已经消耗了它的所有项目。

所以你实际上是在传递一个空的迭代器给zip().

相关内容

  • 没有找到相关文章

最新更新