Python:在嵌套函数中调用管道的recv()方法时,它为什么会阻塞?

2024-05-02 23:44:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我定义了一对函数,其中第二个在第一个函数内部调用,第二个函数为第一个函数中的每个pipe.send()方法关联{}。然而,第一个recv()方法似乎是阻塞的,尽管信息在管道中。我打错顺序了吗?我不太明白问题出在哪里,也不明白在这种情况下recv()方法会挂起。我知道这不是内存问题,因为涉及的数组相对较小,但我想不出可能是什么问题

def plots():

    #build axes and plots

    for i in range(num_sections):
        temp_arr = pipes_hist[1][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):

     #lots of code goes here

     pipes_hist[section_number][1].send(array_section)
     pipes_hist[section_number][1].close()

     #etc etc

     plots()

#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])

for i in range(num_sections):         #these two loops can probably be combined
    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(main_function, args, chunksize=1)

    pool.close()
    pool.join()

编辑:当我将map改为map_async并在后续的pool.map_async之后而不是在main_function之后调用plots函数,也会发生挂起


Tags: 函数insendforetcsectionrangenum