futures (Python 2 Backport of concurrent.futures)
The `futures` library is a backport of the `concurrent.futures` module from Python 3, providing a high-level interface for asynchronously executing callables using thread or process pools. It is designed exclusively for Python 2 environments (specifically 2.6 and 2.7), offering a structured way to manage concurrent tasks in older Python runtimes. The current version is 3.4.0, released in October 2022.
Warnings
- breaking This library is a backport specifically for Python 2.6/2.7. It is *not* compatible with Python 3, where `concurrent.futures` is part of the standard library. Attempting to install or use this package on Python 3 will lead to syntax errors or other runtime issues.
- gotcha The `ProcessPoolExecutor` class in this Python 2 backport has known, unfixable problems and should not be relied upon for mission-critical work. Consider `ThreadPoolExecutor` for I/O-bound tasks or alternative multiprocessing solutions for CPU-bound tasks.
- gotcha Exceptions raised within tasks submitted to an executor are stored in the `Future` object, not immediately re-raised. They will only be re-raised when `future.result()` is called or when iterating over results from `executor.map()` or `as_completed()`. If `future.result()` is never called for a future that failed, the exception may be silently ignored.
- gotcha When using `ThreadPoolExecutor`, be cautious of deadlocks if tasks wait on the results of other tasks submitted to the *same* executor, especially if the pool size (`max_workers`) is insufficient to prevent all workers from becoming blocked simultaneously.
Install
-
pip install futures
Imports
- ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor
- ProcessPoolExecutor
from concurrent.futures import ProcessPoolExecutor
- Future
from concurrent.futures import Future
Quickstart
import time
from concurrent.futures import ThreadPoolExecutor
def my_task(name):
print "Starting task %s" % name # Python 2 print statement
time.sleep(1) # Simulate work
print "Finished task %s" % name # Python 2 print statement
return "Result from %s" % name
if __name__ == '__main__':
# Create a thread pool with 2 workers
with ThreadPoolExecutor(max_workers=2) as executor:
# Submit tasks
future1 = executor.submit(my_task, "Alpha")
future2 = executor.submit(my_task, "Beta")
# Get results (blocks until complete)
print future1.result()
print future2.result()
print "All concurrent tasks completed." # Python 2 print statement