【发布时间】:2020-06-17 17:23:03
【问题描述】:
我有一个执行函数,它应该在多个设备上并行运行。而且我需要在完成每次并行运行时调用不同的函数。我迫不及待地等待所有并行调用完成,因为根据传递的参数需要非常不同的时间。
def func1(device, arg1, arg2):
# do something
for device in devices:
# Call func1 with different arguments in parallel
# If one of the parallel is finished call func2(arg, arg1, arg2) with different arguments.
如何在 Python 中做到这一点?
【问题讨论】:
-
你知道调用
func2的参数吗?或者这些参数是func1的结果? -
也可以是结果,也可以根据知道哪个设备已经完成运行来决定。
-
发送到函数唯一编号并用结果返回它 - 或者用结果返回参数 - 这样你就可以识别过程。
-
@furas 你能不能只写一个伪代码。这对我会有帮助。谢谢。
-
没什么可写的——只有
result, parameters = function(parameters)和def function(parameters): ... return result, parameters
标签: python python-3.x multithreading python-2.7 multiprocessing