Python - 使用 concurrent.futures 阅读地图

Python - Read a map with concurrent.futures

为了减少我的计算时间,在post之后,有人告诉我用concurrent.futures映射。但是我看不到结果,我得到 "generator object map at 0x7f0ef48ff2d0>"...我该怎么做?

import concurrent.futures
import numpy

def f(num):
    return num * 2

arr = numpy.array(
  [numpy.array([
    numpy.array([1,2,3]),
    numpy.array([4,5,6]),
    numpy.array([7,8,9])]),
   numpy.array([
     numpy.array([1,2,3]),
     numpy.array([4,5,6]),
     numpy.array([7,8,9])])])


with concurrent.futures.ProcessPoolExecutor() as exc:
    print(exc.map(f, arr))

调用map return是一个迭代器,它不会return直接产生结果。您可以执行以下操作:

with concurrent.futures.ProcessPoolExecutor() as exc:
    for result in exc.map(f, arr):
        print(result)