运行 同时处理多个并返回结果

Running Multiple Process At The Same Time And Returning the Results

我想同时使用不同的参数执行一个函数,并且还必须在我的 main 中取回 return 值。我的问题的伪代码是

def Function(data):
      print("Function Running With Values" , data[0],"and",data[1],"At timestamp : " ,time.time())
      return data[0]+data[1]


Results= []
Values= [ [1,2],[2,3],[4,5],[1,4]]
#calling function for all Values and return the result in results variable in sequence.

我不确定“同时”是什么意思:

如果顺序处理没问题this

Results = list(map(Function, Values))
print(Results)

或更多pythonic一个list comprehension

Results = [Function(value) for value in Values]
print(Results)

为您提供以下输出

Function Running With Values 1 and 2 At timestamp :  1605276375.4642859
Function Running With Values 2 and 3 At timestamp :  1605276375.4645345
Function Running With Values 4 and 5 At timestamp :  1605276375.4647174
Function Running With Values 1 and 4 At timestamp :  1605276375.4648669
[3, 5, 9, 5]

如果你真的想要多处理那么this

import multiprocessing as mp

with mp.Pool() as p:
    Results = list(p.map(Function, Values))

print(Results)

this

from concurrent.futures import ProcessPoolExecutor

with ProcessPoolExecutor() as p:
    Results = list(p.map(Function, Values))

print(Results)

给你输出像

Function Running With Values 1 and 2 At timestamp :  1605276375.4532914
Function Running With Values 4 and 5 At timestamp :  1605276375.4547572
Function Running With Values 2 and 3 At timestamp :  1605276375.4549458
Function Running With Values 1 and 4 At timestamp :  1605276375.456188
[3, 5, 9, 5]

如果你想要多处理,那么你应该更深入地研究它以确保没有出错并且处理确实更快。但是您的示例是一个经典的 MapReduce 场景,应该可以正常工作。

这就是你要找的吗?