Pool.imap_无序跳过iterable中的值

2024-10-01 09:24:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图运行以下代码来对裁剪geotifs的函数进行视差化。Geotifs的名称为<location>__img_news1a_iw_rt30_<hex_code>_g_gpf_vv.tif。该代码工作得非常好,但它甚至从vv_tif iterable中跳过了一组特定的geotif。特别是,在locationA_img_news1a_iw_rt30_20170314t115609_g_gpf_vv.tiflocationA_img_news1a_iw_rt30_20170606t115613_g_gpf_vv.tiflocationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif中,每当我读取这些文件以及其他位置地理信息时,它都会跳过locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif。但是,如果我仅从这三个geotif文件创建iterable,它将读取此文件。我试过改变chunksize,但没用。我是不是遗漏了什么

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个文件


Tags: 文件代码imgiterablepooliwtifchunksize
2条回答

请注意结果上的差异,这取决于“time.sleep”是输入还是输出

import time
from multiprocessing import Pool

def process(x):
    print(x)

def main():
    pool = Pool(4)
    pool.imap_unordered(process, (1,2,3,4,5))
    pool.close()
    #time.sleep(3)

if __name__ == "__main__":
    main()

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

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

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条线都是一样的。我运行了几次,直到我得到一个不同的订单。在我看来,这是可行的

注意,his证明了各种map函数没有改变底层数据

相关问题 更多 >