We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Concurrency with Threads and asyncio step 4 of 56
Collect Results and Errors from a Thread Pool
You are writing a health check for a list of websites. Some sites answer, some are down, and one bad site must not stop you checking the rest. Run the checks on a thread pool and sort the outcomes into successes and failures.
FakeChecker.check(url) stands in for the real check. It waits a moment,
then returns a status code or raises an exception:
- A URL containing
"down"raisesConnectionError. - A URL containing
"timeout"raisesTimeoutError. - A URL containing
"missing"returns404. Every other URL returns200.
check_all(FakeChecker(), ["ok-1", "down-2", "missing-3"], max_workers=3)
# ({"ok-1": 200, "missing-3": 404},
# {"down-2": "ConnectionError: down-2 is unreachable"})
What to write
Fill in check_all(checker, urls, max_workers). It returns two dictionaries,
(ok, failed):
- Submit
checker.check(url)once for each URL to aThreadPoolExecutorwithmax_workersworkers. - If a check returns, put its status code in
okunder its URL. A404is a result like any other, so it goes inok. - If a check raises, put
"<ExceptionName>: <message>"infailedunder its URL. For example,ConnectionError("down-2 is unreachable")becomes"ConnectionError: down-2 is unreachable". - A failure must not stop the other checks. Every URL ends up in exactly one of the two dictionaries.
The URLs are all different, urls can be empty, and max_workers is at
least 1.
What the driver checks
Keep FakeChecker and solve as they are. solve returns your two
dictionaries as ok and failed, plus:
calls: how many timescheckran. It must equal the number of URLs.overlapped:Trueif two or more checks ran at the same time. With one URL or one worker, the expected value isFalse.
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.