24.6 SECONDS, 0.117 OF THEM WORK
- waiting for servers24.47 sthe dim bar: no CPU used
- working0.117 s · 0.5%the thin ticks: time.process_time() for the whole run
The plain version reads links.txt and calls urllib.request.urlopen on each link in turn, with a 5-second timeout. Each call is a : it does not return until the server has answered, and the program’s one thread can do nothing else in the meantime.
Timed, the loop takes 24.59 seconds. time.process_time(), which counts only the time the processor spent on this program, says 0.117 seconds. The program worked for 0.5% of the run; for the rest it was waiting for another program, the server, to answer.
import time
import urllib.error
import urllib.request
urls = open("links.txt").read().split() # 100 links
start = time.perf_counter()
for url in urls:
try:
with urllib.request.urlopen(url, timeout=5) as resp:
resp.read() # the thread waits here
except urllib.error.HTTPError:
pass # 404 and 500 still answered
except (urllib.error.URLError, TimeoutError):
pass # refused, or silent for 5 s
print(f"{time.perf_counter() - start:.2f} s") # 24.59 s, 0.117 s of it CPU