首页 > 解决方案 > Pool.imap_unordered 从可迭代对象中跳过值

问题描述

我正在尝试运行以下代码来并行化裁剪 geotifs 的函数。Geotif 被命名为<location>__img_news1a_iw_rt30_<hex_code>_g_gpf_vv.tif. 该代码工作得非常好,但它甚至从 vv_tif 可迭代的读取中跳过了一组特定的 geotif。特别是 out of locationA_img_news1a_iw_rt30_20170314t115609_g_gpf_vv.tif,当我阅读这些文件以及其他位置 geotif 时locationA_img_news1a_iw_rt30_20170606t115613_g_gpf_vv.tif,它每次都会locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif跳过阅读。locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif但是,如果我只从这三个 geotif 文件创建一个可迭代对象,它就会读取这个文件。我曾尝试更改块大小,但没有帮助。我在这里错过了什么吗?

from multiprocessing import Pool, cpu_count
try:
    pool = Pool(cpu_count())
    pool.imap_unordered(tile_geotif, vv_tif, chunksize=11)
finally:
    pool.close()

编辑:我总共有 55 个文件,它locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif每次只删除文件。

标签: pythonpython-3.xpython-multiprocessing

解决方案


这太多了,无法在评论中显示,在这里回答。

在我看来,地图功能在我下面的玩具示例中起作用。我认为您的输入数据有错误导致输出损坏。要么,要么你发现了一个错误。如果是这样,请尝试创建一个可重现的示例。

from multiprocessing import Pool

vv_tif = list(range(10))
def square(x):
    return x**x

with Pool(5) as p:
    print(p.map(square, vv_tif))

with Pool(5) as p:
    print(list(p.imap(square, vv_tif)))

with Pool(5) as p:
    print(list(p.imap_unordered(square, vv_tif)))

with Pool(5) as p:
    print(list(p.imap_unordered(square, vv_tif, chunksize=11)))

输出:

[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]
[1, 1, 256, 3125, 46656, 823543, 16777216, 4, 27, 387420489]
[1, 1, 4, 27, 256, 3125, 46656, 823543, 16777216, 387420489]

通常所有 4 行都是相同的。我跑了几次,直到我得到一个不同的订单。在我看来它有效。

请注意,他证明了各种map函数不会改变基础数据。


推荐阅读