Python Using List/Multiple Arguments in Pool Map

前端 未结 3 1623
清酒与你
清酒与你 2021-02-20 14:13

I am trying to pass a list as a parameter to the pool.map(co_refresh, input_list). However, pool.map didn\'t trigger the function co_refresh

3条回答
  •  死守一世寂寞
    2021-02-20 14:44

    You should define your work function before declaring the Pool, when you declaring Pool, sub worker processes forked from that point, worker process don't execute code beyond that line, therefore not seeing your work function.

    Besides, you'd better replace pool.map with pool.starmap to fit your input.

    A simplified example:

    from multiprocessing import Pool
    
    def co_refresh(a, b, c, d):
        print(a, b, c, d)
    
    input_list = [f'a{i} b{i} c{i} d{i}'.split() for i in range(4)]
    # [['a0', 'b0', 'c0', 'd0'], ['a1', 'b1', 'c1', 'd1'], ['a2', 'b2', 'c2', 'd2'], ['a3', 'b3', 'c3', 'd3']]
    
    pool = Pool(processes=3)
    pool.starmap(co_refresh, input_list)
    pool.close()
    

提交回复
热议问题