Python Glossary Русский

Python 3.2+concurrent.futuresterm

concurrent.futures.ThreadPoolExecutor

thread pool

A pool of threads from the concurrent.futures module. It handles creating and reusing the threads. Convenient for parallel I/O work.

Syntax

with ThreadPoolExecutor(max_workers=N) as ex:
    future = ex.submit(fn, *args)
    results = ex.map(fn, iterable)

CPython threads share one GIL, so a thread pool only speeds up waiting — network, disk, sleep; for pure computation it buys nothing and you want ProcessPoolExecutor instead. Leaving the with block calls shutdown(wait=True) and blocks until every task is done, and an exception raised inside a task stays parked in its Future until you touch result(). With max_workers left out, the thread count is derived from the CPU count and capped at 32.

Examples

from concurrent.futures import ThreadPoolExecutor
import time
urls = ['https://python.org', 'https://pypi.org']
def fetch(url):
    time.sleep(0.1)  # имитация ожидания сети: пока один поток ждёт, работают другие
    return len(url)
with ThreadPoolExecutor(max_workers=4) as ex:
    print(list(ex.map(fetch, urls)))  # → [18, 16] — порядок результатов как у входа

See also

Open in the full glossarydocs.python.orgReport an inaccuracy