How to Use ThreadPoolExecutor in Python for Parallel Processing

Use ThreadPoolExecutor with executor.map to run a function over many inputs concurrently and collect ordered results.

Easy Python 3.9+ Aug 9, 2026 Concurrency & performance 13 views 0 copies

Python code

10 lines
Python 3.9+
def worker(item):
    return item * item

if __name__ == "__main__":
    from concurrent.futures import ThreadPoolExecutor
    numbers = list(range(1, 11))
    with ThreadPoolExecutor(max_workers=4) as executor:
        results = list(executor.map(worker, numbers))
    print("Input:  ", numbers)
    print("Results:", results)

Output

stdout
Input:   [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Results: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

How it works

The concurrent.futures.ThreadPoolExecutor manages a pool of worker threads. Executor.map submits each item from the iterable to the pool and returns results in the same order as the input, preserving determinism. Using the context manager ensures threads are joined and resources cleaned up even if an exception occurs. The function 'worker' squares each number, but this pattern works for any CPU-bound or I/O-bound helper.

Common mistakes

  • Using ThreadPoolExecutor for CPU-bound tasks; GIL limits speedup, consider ProcessPoolExecutor.
  • Forgetting to wrap in a context manager, leaving threads unmanaged.
  • Assuming results are returned in input order; map ensures order but async can scramble.
  • Creating a new executor for every call; reuse one for multiple batches.

Variations

  1. Use as_completed to process results as they finish, not in input order.
  2. Switch to ProcessPoolExecutor for true parallelism on CPU-heavy work.

Real-world use cases

  • Fetches multiple URLs concurrently in a web scraper, collecting responses in posts order.
  • Processes a batch of image thumbnails in parallel using threads for I/O-like file operations.
  • Runs several database queries simultaneously and aggregates the results in one flow.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Concurrency & performance

Related tutorials and quizzes for this topic.