Using Futures

Learn to use futures in Python.

Background

Python 3.2 introduced the concurrent.futures module, which provides an easy way to schedule asynchronous tasks.

The module is also available in Python 2 as it has been back-ported. It can easily be installed by running pip install futures.

The concurrent.futures module is pretty straightforward to use. First, one needs to pick an executor. An executor is responsible for scheduling and running asynchronous tasks. It can be seen as a type of engine for execution. The module currently provides two kinds of executors: concurrent.futures.ThreadPoolExecutor and concurrent.futures.ProcessPoolExecutor. As one might guess, the first one is based on threads and the second one on processes.

As outlined in the first chapter, the process-based executor is going to be much more efficient for long-running tasks that benefit from having an entire CPU available. The threading executor suffers from the same limitation as the threading module, which was covered earlier.

...