Thread Pool
hardpy_threadsImplement a reusable ThreadPool that executes submitted callables across a fixed number of background worker threads, returning Future handles for asynchronous result retrieval.
The concurrent and multiprocessing modules are off-limits.
Interface
Future:result(timeout: float | None = None) -> Any: Block until the computation finishes and return its return value. If the task raised an exception, re-raise that exception here. Iftimeoutis given and expires before completion, raiseTimeoutError.exception(timeout: float | None = None) -> BaseException | None: Block until the computation finishes and return the exception raised, orNoneif it succeeded. Iftimeoutexpires, raiseTimeoutError.done() -> bool: ReturnTrueif the computation has finished (succeeded or raised).
ThreadPool(num_workers: int):submit(fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Future: Submit a task to be executed by a worker. Return aFuturerepresenting the pending result. If called aftershutdown(), raiseRuntimeError.shutdown(wait: bool = True) -> None: Signal workers to shut down. No further tasks can be submitted. Ifwait=True, block until all pending tasks and worker threads have finished.- Context manager:
Can be used in a
with ThreadPool(...) as pool:block; callsshutdown(wait=True)on exit.
- no concurrent, multiprocessing
Your solution
Edit ThreadPool and run the real pytest suite in your browser — no install required. Your code is saved locally.
Loading the Python runtime (first run only)…
Hints
Hint 1
Create worker threads that loop pulling tasks from a work queue until a sentinel object (e.g. None) is received.
Hint 2
Future can use threading.Event to signal when the task has produced a result or raised an exception.
Hint 3
When a worker encounters an exception from a task, catch BaseException and store it in the Future so future.result() can re-raise it.
Hint 4
submit() should check if the pool is shutdown and raise RuntimeError if so.
Hint 5
shutdown(wait=True) should enqueue one sentinel per worker thread and join all worker threads.