Python futures

From wikinotes

Python's futures module introduces the concept of Executors and Futures. It reduces the overhead required to run asynchronous code.

  • An executor is initialized to manage a threadpool/processpool.
  • Each job submissions returns a future object.


See python concurrency for more concurrency options and concurrency primitives.

Executors

ThreadPoolExecutor, ProcessPoolExecutor, ...

ThreadPoolExecutor

from concurrent import futures

with futures.ThreadPoolExecutor(max_workers) as ex:
    result = ex.map(func, args)     # distributes execution of `map` over 3 threads
    future = ex.submit(func, args)  # runs `func` in one thread

See excellent: http://chriskiehl.com/article/parallelism-in-one-line/