Pool.imap_unordered 跳过迭代中的值
Pool.imap_unordered skips value from the iterable
我正在尝试 运行 以下代码来并行化裁剪 geotif 的函数。 Geotifs 被命名为 <location>__img_news1a_iw_rt30_<hex_code>_g_gpf_vv.tif
。代码工作得很好,但它跳过了一组特定的 geotif,甚至从 vv_tif 可迭代中读取。特别是,在 locationA_img_news1a_iw_rt30_20170314t115609_g_gpf_vv.tif
、locationA_img_news1a_iw_rt30_20170606t115613_g_gpf_vv.tif
和 locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif
中,当我读取这些文件以及其他位置 geotifs 时,它每次都会跳过 locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif
。但是,如果我仅从这三个 geotif 文件创建可迭代对象,它会读取此文件。我试过更改 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
个文件。
评论太多了,放在这里回答。
在我看来,地图函数在我下面的玩具示例中有效。我认为您的输入数据有误会导致输出损坏。要么,要么你发现了一个错误。如果是这样,请尝试创建一个可重现的示例。
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
函数不会改变基础数据。
请注意结果的差异取决于“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()
我正在尝试 运行 以下代码来并行化裁剪 geotif 的函数。 Geotifs 被命名为 <location>__img_news1a_iw_rt30_<hex_code>_g_gpf_vv.tif
。代码工作得很好,但它跳过了一组特定的 geotif,甚至从 vv_tif 可迭代中读取。特别是,在 locationA_img_news1a_iw_rt30_20170314t115609_g_gpf_vv.tif
、locationA_img_news1a_iw_rt30_20170606t115613_g_gpf_vv.tif
和 locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif
中,当我读取这些文件以及其他位置 geotifs 时,它每次都会跳过 locationA_img_news1a_iw_rt30_20170712t115615_g_gpf_vv.tif
。但是,如果我仅从这三个 geotif 文件创建可迭代对象,它会读取此文件。我试过更改 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
个文件。
评论太多了,放在这里回答。
在我看来,地图函数在我下面的玩具示例中有效。我认为您的输入数据有误会导致输出损坏。要么,要么你发现了一个错误。如果是这样,请尝试创建一个可重现的示例。
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
函数不会改变基础数据。
请注意结果的差异取决于“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()