We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Modules, Packaging and Tooling step 18 of 41
Close Only What You Created
Fix BatchProcessor so it shuts down only an executor it created. If the
caller passes an executor in, the caller still owns it, so the processor must
leave it running.
with BatchProcessor(format_item, workers=2) as p: # creates its own pool
p.run([1, 2]) # ["#1", "#2"]
# the processor shut its pool down here
with ThreadPoolExecutor(max_workers=2) as pool:
with BatchProcessor(format_item, executor=pool) as p: # borrows pool
p.run([1, 2])
pool.submit(len, "abcd").result() # 4, the pool is still open
What to write
Keep the class BatchProcessor[T, R] and its typed signatures, including the
__exit__ annotations. It stores the callback fn and has two public
attributes, executor and owns_executor.
- If
executorisNone, createThreadPoolExecutor(max_workers=workers)and setowns_executortoTrue. Let any error from creating the pool propagate. - Otherwise keep the given executor as it is, and set
owns_executortoFalse. In this caseworkersdoes nothing. __enter__returnsself, typed asSelf.__exit__callsshutdown(wait=True)only whenowns_executorisTrue. It returnsNone, so an exception raised in thewithblock propagates.run(items)returnslist(self.executor.map(self._fn, items)). That keeps the input order and any duplicates, and empty input gives[].- Do not change
executororowns_executorafter__init__.
The starter already maps values correctly. Its bugs are that it always sets
owns_executor to True, and it always shuts the executor down.
The driver
solve(values, provide_executor, workers) is already written. Keep it as it
is. It returns the tuple (results, owns_executor, probe):
- If
provide_executorisTrue, the driver creates a pool withworkersthreads and passes it in, so the processor borrows it. Otherwise it passesNone. resultsis whatrun(values)returned inside thewithblock.probetests the executor after thewithblock ends. The driver submitslen("abcd")and records4if that works, or-1ifsubmitraisesRuntimeError. A pool raises that aftershutdown().- The driver shuts down its own pool in a
finallyblock.
solve([2, -1, 2], False, 2) # (["#2", "#-1", "#2"], True, -1)
solve([2, -1, 2], True, 2) # (["#2", "#-1", "#2"], False, 4)
solve([], True, 1) # ([], False, 4)
Your solution must pass mypy --strict.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.