参考一如故往博客:https://blog.csdn.net/qq_37616069/article/details/79831013?utm_source=blogxgwz5
使用concurrent.futures中ProcessPoolExecutor创建进程池,需要使用Queue来完成进程通信时,需要使用multiprocessing.Manager()中的Queue(),而不是multiprocessing.Queue(),否则会报错。
示例代码:
if __name__ == "__main__":
result = findSameId(file_path)
print(len(result))
q = Manager().Queue(len(result))
for key in result:
q.put(key)
if not os.path.exists(new_train_data_dir):
os.makedirs(new_train_data_dir)
executor = ProcessPoolExecutor(max_workers=process_num)
task_list = [executor.submit(saveNpy, train1_path,train2_path, result,new_train_data_dir,q) for n in range(6)]
def saveNpy(train1_path,train2_path,id_file,save_path,q):
if not q.empty():
i = 0
for id_name in id_file[q.get()]:
print(id_name)
# dir_path = os.path.join(base_path,id_name)
dir_path = findThePath(train1_path,train2_path,id_name)
patient = load_scan(dir_path)
patient_pixels = get_pixels_hu(patient)
# print(patient_pixels.shape)
pixel_resample,new_spacing = resample(patient_pixels,patient,[1,1,1])
# print(pixel_resample.shape)
if i == 0:
pixel_resample_copy = pixel_resample.copy()
start_num,end_num = maxSliceIndex(pixel_resample,dir_path)
images = getSlices(pixel_resample_copy,start_num,end_num)
i = 1
else:
images = getSlices(pixel_resample,start_num,end_num)
# pixel_resample_copy = pixel_resample.copy()
# start_num,end_num = maxSliceIndex(pixel_resample,dir_path)
# images = getSlices(pixel_resample_copy,start_num,end_num)
images = changeLevelWidth(images,60,350)
np.save(os.path.join(save_path, id_name), images)
return None