Run independent callables through a thread pool.

Prototype: submit_one(function, value) creates a ThreadPoolExecutor with one worker, submits one call, returns its result, and shuts down the executor. Worker exceptions propagate. The callable must execute in a worker thread.

Final: map_in_threads(function, values, workers=2) executes one call per value using a ThreadPoolExecutor with at most workers workers and returns a list of results in input order. workers is an integer and must be positive, even for empty input; otherwise raise ValueError. Empty input returns []. Duplicates produce separate calls. Do not mutate the input. Propagate a worker exception to the caller and shut down the executor on success or failure. All submitted work must be finished when the function returns or raises. No cancellation policy or timing guarantee is required. Example: (abs, [-2, 0, -1]) -> [2, 0, 1].

Drag or use the arrow keys to resize. Home collapses the question; End collapses the editor. Enter or double-click resets the split.
Loading editor…
Run this exercise with local Python.

This exercise starts native threads or processes, which browser Python does not support. Download your code and tests, then run the command below.

python3 main.py