Standard Library

Threads and Processes

Threads share memory, while processes run in separate interpreters.

ThreadPoolExecutor runs square across two worker threads sharing the same interpreter (and the GIL); ProcessPoolExecutor runs pow across two child processes with isolated memory. Each pool.map returns an iterator over results in input order, and the surrounding with block joins the workers when the body exits. (This fragment runs in standard Python only — the Python By Example runner does not provide native threads or child processes.)

Standard Python

with ThreadPoolExecutor(max_workers=2) as pool:
    print(list(pool.map(square, [1, 2, 3])))

with ProcessPoolExecutor(max_workers=2) as pool:
    print(list(pool.map(pow, [4, 5], [2, 2])))
THREADS: SHARED INTERPRETERGILTHREAD ATHREAD BPROCESSES: ISOLATEDproc Aproc B
Threads share memory but the GIL serialises Python bytecode; processes run in parallel with isolated memory.

A thread pool runs ordinary callables while sharing memory with the current process. map() returns results in input order.

Source

from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor


def square(number):
    return number * number

with ThreadPoolExecutor(max_workers=2) as pool:
    print(list(pool.map(square, [1, 2, 3])))

Output

[1, 4, 9]

A process pool uses separate Python processes. That boundary is heavier, but it can run CPU-bound work outside the current interpreter.

Source

print(ProcessPoolExecutor.__name__)

Output

ProcessPoolExecutor

Notes

See also

Run the complete example

Example code

Expected output

[1, 4, 9]
ProcessPoolExecutor

Execution time appears here after you run the example.