Python多处理未运行

问题描述

我尝试在此for循环上使用多重处理:

def segment_features(segment_pixels):
    features = []
    npixels,nbands = segment_pixels.shape
    for b in range(nbands):
        stats = scipy.stats.describe(segment_pixels[:,b])
        band_stats = list(stats.minmax) + list(stats)[2:]
        if npixels == 1:
            # in this case the variance = nan,change it 0.0
            band_stats[3] = 0.0
        features += band_stats
    return features

segment_ids = np.unique(segments)
objects = []
object_ids = []

for id in segment_ids:
    segment_pixels = img[segments == id]
    object_features = segment_features(segment_pixels)
    objects.append(object_features)
    object_ids.append(id) 

通过用以下替换for循环部分:

def segment_features_calc(segment_pixels):
    segment_pixels = img[segments == id]
    object_features = segment_features(segment_pixels)
    objects.append(object_features)
    object_ids.append(id)
    print("segment "+str(id)+" features calculated")
n=mp.cpu_count()
if__name__ == '__main__':
    p = mp.Pool(processes = n)
    start = time.time()
    async_result = p.map_async(segment_features_calc,list(segment_ids))
    p.close()
    p.join()
    print("Complete")
    end = time.time()
    print('total time (s)= ' + str(end-start))

但是,多重处理无法正确执行(多重处理在0.17秒后停止,而要循环的项目接近270,000+个段ID)。关于如何解决此问题有何见解?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)