在我的代码中,有许多函数被称为func1、func2、func3等等……我使用多处理池来调用这些函数,并使它们并行运行。代码如下:
def func1():
...
def func2():
...
if __name__ == '__main__':
with Pool(processes=2) as pool:
r1 = pool.apply_async(func1, ())
r2 = pool.apply_async(func2, ())
var1 = r1.get(timeout=20)
var2 = r2.get(timeout=20)
我需要增加函数的数量,我想用一些循环之王来调用这些函数。这是怎么做到的?
将列表用于附加的间接层。
if __name__ == '__main__':
with Pool(processes=2) as pool:
funcs = [func1, func2]
rs = [pool.apply_async(func, ()) for func in funcs]
vars = [r.get(timeout=20) for r in rs]