我的代码通常依赖于两个函数,一个用于在多次迭代中处理大块数据,另一个用于在每次迭代结束时生成一系列图。为了加快代码速度,我试图以Pools和Pipes的形式使用Python的多处理功能,这样我就可以将大函数分解成几个部分,然后将这些部分拼接在一起并生成图形。用那个输出。然而,我的管道的recv()
方法似乎并没有实际发挥作用,而且我很难理解为什么。
def plots():
#build axes and plots
for i in range(num_sections):
temp_arr = pipes_hist[i][0].recv() #the KeyboardInterrupt traceback message points here
arr_section += temp_arr #in my actual code arr_section is predefined with the proper size
#etc etc
def main_function(section_number):
for m in range(num_iterations):
#lots of code goes here
pipes_hist[section_number][1].send(array_section)
pipes_hist[section_number][1].close()
#etc etc
#build list of pipes for each section
pipes_hist_send = [[] for i in range(num_sections)]
pipes_hist_recv = [[] for i in range(num_sections)]
pipes_hist = list(zip(pipes_hist_recv,pipes_hist_send))
for i in range(num_sections):
pipes_hist[i] = list(pipes_hist[i])
pipes_hist[i][0],pipes_hist[i][1] = Pipe()
#begin multiprocessing
if __name__ == '__main__':
pool = mp.Pool(num_sections)
args = np.arange(num_sections)
pool.map_async(main_function, args, chunksize=1)
for m in range(num_iterations):
plots()
pool.close()
pool.join()
代码在.recv()
的{{1}}方法中挂起。我完全难倒了,关于可能发生的事情,文档非常不透明。任何和所有的帮助或见解将不胜感激