How can I recover the return value of a function passed to multiprocessing.Process?

前端 未结 12 1637
野的像风
野的像风 2020-11-22 07:36

In the example code below, I\'d like to recover the return value of the function worker. How can I go about doing this? Where is this value stored?

12条回答
  •  情话喂你
    2020-11-22 08:08

    A simple solution:

    import multiprocessing
    
    output=[]
    data = range(0,10)
    
    def f(x):
        return x**2
    
    def handler():
        p = multiprocessing.Pool(64)
        r=p.map(f, data)
        return r
    
    if __name__ == '__main__':
        output.append(handler())
    
    print(output[0])
    

    Output:

    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    

提交回复
热议问题