问题描述
最近在解决一个并行化的问题时,我需要在多个子进程中计算得出的numpy矩阵供主进程加起来求和,但是实际中发现 multiprocessing中的Queue似乎对numpy的数据的处理有些bug。
以下是一份demo代码,开始的想法是在多个子进程中直接将计算的矩阵put进Queue中,父进程阻塞,等子进程计算完后,再在父进程中将矩阵一个个get出来再相加,下面这份代码应该是能正常跑的。
import numpy as np
import multiprocessing
from multiprocessing import Queue
import time
def checkWrong(resultQueue, t):
while t:
mat = np.ones((10,1))
time.sleep(0.5)
resultQueue.put(mat)
t -= 1
if __name__ == '__main__':
resultQ = Queue()
t = 10
proc1 = multiprocessing.Process(target=checkWrong, args = (resultQ,