Two functions in parallel with multiple arguments and return values

后端 未结 3 1066
夕颜
夕颜 2021-02-06 04:09

I\'ve got two separate functions. Each of them takes quite a long time to execute.

def function1(arg):
     do_some_stuff_here
     return result1

def function2         


        
3条回答
  •  感情败类
    2021-02-06 04:45

    This is another example I just found, hope it helps, nice and easy ;)

    from multiprocessing import Pool
    
    def square(x):
        return x * x
    
    def cube(y):
        return y * y * y
    
    pool = Pool(processes=20)
    
    result_squares = pool.map_async(square, range(10))
    result_cubes = pool.map_async(cube, range(10))
    
    print result_squares.get(timeout=3)
    print result_cubes.get(timeout=3)
    

提交回复
热议问题