1

多处理新手希望使用 numpy。

我有一个脚本,它分割图像并将图像的可变大小块创建为 numpy 数组。

np_array = gdal.ReadAsArray()

过去我已经串行处理了这个数组,没有问题。从这里的许多帖子看来,我最好的选择是将数组转换为 ctypes,对其进行切片,然后将切片发送到多个 multiprocessing.Pool。我有一个要分割的多波段图像,所以下面的代码在 main()

        Open the dataset...
        Get the first band...
        Grab a small segment of that band (these are huge images) and open to nparray

        #Convert to ctypes to allow multiprocessing
        c_pointer = ctypes.POINTER(ctypes.c_byte) #Here I need a dict of gdal to ctypes conversions.
        shared_array = np_array.ctypes.data_as(c_pointer)
        shared_array.reshape(intervalx, intervaly) 

        def my_func(i, def_param=shared_array):
            #perform my stretch here
            pass

        pool = multiprocessing.Pool()
        pool.map(my_func, range(10))

        print shared_array

SO - 链接

我理解需要有 def 语句中间代码,因为我将 shared_array 作为参数传递。有没有更好的办法?

在这一点上,我的代码正在崩溃......很难。我错过了什么?这不是使用 numpy 数组处理并行处理类型的方法吗?

最后,这些是图像,我需要能够保持数组的顺序。这可能吗,还是我需要使用锁?如果是这样,来自 numpy 或多处理。

任何指向信息的链接都表示赞赏,试图学习如何在共享内存空间中处理多处理 numpy 数组。

PS 如果可能,我宁愿避免使用 numpy_sharedmem 模块,因为我想限制潜在用户的额外下载次数。

4

0 回答 0