← All Exercises

Thread Pool Fan-Out with ThreadPoolExecutor

intermediatepython~25min

Background

Threads release the GIL during blocking I/O, so a bounded pool can overlap waits without spawning one thread per request.

Problem

Given jobs shaped as (url, latency_seconds, byte_size), simulate each fetch with time.sleep and return all byte sizes in the original order using a thread pool with at most workers threads. Validate that workers is at least one and that latency is non-negative.

Input

A list of (url, latency, byte_size) tuples and a positive worker limit.

Output

A list of byte sizes in the same order as the input jobs.

Sample tests

Sample 1

Input: jobs = [('/a', 0.05, 1200), ('/b', 0.01, 800)], workers = 2
Expected: [1200, 800]

Sample 2

Input: jobs = [('/x', 0.0, 42)], workers = 1
Expected: [42]

Your solution

Learning objective

Use ThreadPoolExecutor.map for ordered concurrent I/O and understand bounded parallelism.