Python 如何与并发模块一起使用共享内存?

Python 如何与并发模块一起使用共享内存?,python,multithreading,numpy,multiprocessing,Python,Multithreading,Numpy,Multiprocessing,我使用并发模块来并行化一个函数,但速度很慢 with concurrent.futures.ThreadPoolExecutor() as executor: images = executor.map(gen_images, paths) 我可以使用共享内存来加速此过程吗?函数返回的数组是numpy数组 编辑:要并行化的函数如下: 该功能包括读取和处理。但这比阅读要复杂得多。所以,我认为它是CPU限制的 def gen_images(heads): N = len(h

我使用并发模块来并行化一个函数,但速度很慢

with concurrent.futures.ThreadPoolExecutor() as executor:
    images = executor.map(gen_images, paths)    
我可以使用共享内存来加速此过程吗?函数返回的数组是numpy数组

编辑:要并行化的函数如下: 该功能包括读取和处理。但这比阅读要复杂得多。所以,我认为它是CPU限制的

def gen_images(heads):
    N = len(heads)
    # we will be taking 34 slices from the head
    images = np.zeros((N * 34, 328, 286), dtype='uint8')
    counter = 0

    for i in tqdm(heads):
        name = i
        # reading the associated head
        insert_path = os.path.join(name, 'insertion_metadata.txt')
        model_path = glob(os.path.join(name, 'head', '*' + '.dat'))[0]
        data_, specs_, insertion_ = read_head(model_path, insert_path)
        for z in range(-17, 17):
            images[counter ,: ,:] = plot_slice(data_, specs_, insertion_, return_image = True, z_slice=z)
            counter += 1
    return images

您正在使用线程池。线程已经自动共享内存了。@user2357112supportsMonica那么您如何解释我使用多处理还是多线程的速度是相同的?谁知道呢。也许你搞砸了。也许这两种方法的不同开销来源恰好彼此非常接近,以至于你看不到它们之间的区别。我们无法从中分辨。@user2357112supportsMonica我已经添加了函数。我不知道并发模块是如何利用共享内存的。函数的每个实例都返回一个单独的输出,这些输出被累积在一个列表中并返回。