August 2018
Intermediate to advanced
366 pages
10h 14m
English
ThreadPool is particularly convenient when you have multiple entries to which you need to apply the same operation over and over. Suppose you have a list of four URLs that you need to download:
urls = [
"https://httpbin.org/delay/1",
"https://httpbin.org/delay/2",
"https://httpbin.org/delay/3",
"https://httpbin.org/delay/4"
]
Fetching them in a single thread would take a lot of time:
def fetch_all_urls():
contents = []
for url in urls:
contents.append(fetch_url(url))
return contents
We can test the time by running the function through the timeit module:
>>> import timeit >>> timeit.timeit(fetch_all_urls, number=1) 12.116707602981478
If we could do so using a separate thread for each function, it would only take the time of ...