Python 3.2+concurrent.futuresterm
concurrent.futures.Executor
task pool
The abstract base class of executor pools: the common submit, map and shutdown interface for thread, process and interpreter pools.
Syntax
def run_all(pool: concurrent.futures.Executor): ... # submit, map, shutdown
Executor itself runs nothing — its submit raises NotImplementedError; you create a subclass and use Executor in type hints so the pool can be swapped. map yields results in input order, not completion order, and the first task error surfaces during iteration and stops it. shutdown(wait=False) does not stop tasks already running: the program still waits for them before exiting.
Examples
from concurrent.futures import Executor, ThreadPoolExecutor
def squares(pool: Executor, data):
return list(pool.map(lambda x: x * x, data))
with ThreadPoolExecutor() as pool:
print(squares(pool, [3, 1, 2])) # → [9, 1, 4]
from concurrent.futures import ThreadPoolExecutor
def inv(x):
return 1 / x
with ThreadPoolExecutor() as pool:
try:
for r in pool.map(inv, [1, 0, 2]):
print(r)
except ZeroDivisionError:
print('ошибка во второй задаче') # → 1.0 / ошибка во второй задаче
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy