我有以下代码,但无法从迭代器中得到结果
from multiprocess import freeze_support
from pathos.multiprocessing import ProcessPool
if __name__ == "__main__":
freeze_support()
pool = ProcessPool(nodes=4)
results = pool.uimap(pow, [1,2,3,4], [5,6,7,8])
print("...")
print(list(results))
代码没有出错,只是挂起。
实现这一点有一些微妙之处,但简短的版本是imap或uimap。它们是迭代器,不同于python多处理示例中的map。要提取结果,它需要在for循环中。如果在类中,您还需要调用的方法是@staticmethod
from multiprocessing import freeze_support
from multiprocessing import Pool
def f(vars):
return vars[0]**vars[1]
if __name__ == "__main__":
freeze_support()
pool = Pool(4)
for run in pool.imap(f, [(1,5), (2,8), (3,9)]):
print(run)