Python - 使用concurrent.futures读取地图

时间:2015-04-28 13:39:42

标签: python multithreading concurrent.futures

为了减少我的计算时间,在下面的帖子中,有人告诉我使用带有concurrent.futures的map。但我无法读取结果,我得到“生成器对象图在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))

1 个答案:

答案 0 :(得分:2)

调用map返回一个迭代器,它不会直接返回结果。以下是您可以做的事情:

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