Search code examples
pythonmultithreadingconcurrent.futures

Python - Read a map with concurrent.futures


In order to decrease the time of my calculations, in following post, someone told me to use map with concurrent.futures. But i can t read the results, i get "generator object map at 0x7f0ef48ff2d0>"...how can i do that?

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))

Solution

  • Calling map returns an iterator, it does not return result directly. Here's what you could do:

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