A ThreadPoolExecutor runs submitted callables in worker threads and returns futures that carry either results or failures.
Python thread pools: collect results and observe worker failures
Operation contract
The import submits two valid amount checks and one rejected amount to a two-worker pool. It calls result on every future, so the worker exception is observed rather than left in an ignored future. It sorts accepted results for deterministic display, keeping submission order separate from possible completion order.
Failure and ownership boundary
Two workers bound concurrent execution, not the number of queued submissions. Submitting an entire huge import can still retain a future and input for each record. Threads are also not an automatic CPU-parallel speedup across Python builds; the fixture makes no GIL or throughput claim. Python asyncio TaskGroup: cancel sibling work and retain failure evidence solve a different scheduling problem.
Working program
from concurrent.futures import ThreadPoolExecutor
def checked_amount(amount):
if amount <= 0:
raise ValueError("positive amount required")
return amount * 2
accepted = []
rejected = 0
with ThreadPoolExecutor(max_workers=2) as workers:
futures = [workers.submit(checked_amount, amount) for amount in [125, 75, -1]]
for future in futures:
try:
accepted.append(future.result())
except ValueError:
rejected += 1
print(sorted(accepted))
print("rejected:", rejected)Output
[150, 250]
rejected: 1Costs and limits
This fixture retains three futures. A submit-all pattern retains O(n) futures for n inputs even with a small worker count; use bounded admission for large imports.
Common Mistakes
- Read every future result or define an explicit error sink.
- Worker count is not a queue-size bound.
Connected lessons
Python asyncio TaskGroup: cancel sibling work and retain failure evidence, Python exceptions: translate an input error without hiding its cause, Java bounded executors: test saturation and rejected work.
Apply this boundary
Python locks: protect the complete inventory transition, Python process pools: importable workers and serialization costs.
