因此,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_v