我正在尝试在 python 中运行一个并行进程,其中我必须根据某些条件从一个大数组中提取某些多边形。大数组有 10k+ 个被索引的多边形。
在extract_polygon
我传递的函数中(数组,索引)。根据索引,函数必须返回与该索引对应的多边形,或者不根据定义的条件返回。该数组永远不会更改,仅用于根据提供的索引读取多边形。
由于数组非常大,我在并行处理过程中遇到内存不足错误。我怎样才能避免呢?(在某种程度上,如何在多处理中有效地使用共享数组?)
下面是我的示例代码:
def extract_polygon(array, index):
try:
islays = ndimage.find_objects(clone==index)
poly = clone[islays[0][0],islays[0][1]]
area = np.count_nonzero(ploy)
minArea = 100
maxArea = 10000
if (area > minArea) and (area < maxArea):
return poly
else:
return None
except:
return None
start = time.time()
pool = mp.Pool(10)
results = pool.starmap(get_objects,[(array, index) for index in indices])
pool.close()
pool.join()
#indices here is a list of all the indexes we have.
ray
在这种情况下,我可以使用任何其他库吗?