python 并行化 图像处理 | 您所在的位置:网站首页 › numpy并行运算 › python 并行化 图像处理 |
因此,Python的多处理工作方式(在大多数情况下)是必须指定要运行的各个线程。我在这里做了一个简短的介绍教程:http://will-farmer.com/parallel-python.html 在您的例子中,我建议将tris分成一组不同的部分,每个部分大小相等,每个部分代表一个“worker”。您可以使用numpy.split()拆分此列表(此处的文档:http://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html)。在 然后,对于tri中的每个列表,我们使用线程和队列模块来指定8个工人。在import numpy as np # split into 8 different lists tri_lists = np.split(tris, 8) # Queues are threadsafe return_values = queue.Queue() threads = [] def color_image(q, tris, hipo, tridex): """ This is the function we're parallelizing """ for tri in tris: return_values.put(np.mean(hipo[tridex==tri,:], axis=0)) # Now we run the jobs for i in range(8): threads.append(threading.Thread( target=color_image, args=(return_values, tri_lists[i], hipo, tridex))) # Now we have to cleanup our results # First get items from queue results = [item for item in return_values.queue] # Now set values in lopo for i in range(len(results)): for t in tri_lists[i]: lopo[tridex==t, :] = results[i] 这不是最干净的方法,我不确定它是否有效,因为我无法测试它,但这是一个不错的方法。并行化部分现在是np.mean(),而设置值不是并行化的。在 如果您还想并行化这些值的设置,那么必须有一个共享变量,可以使用队列,也可以使用全局变量。在 |
CopyRight 2018-2019 实验室设备网 版权所有 |