I have an example code below where I download several hundred files in different thread from a list of urls. The download of each file can take several minutes. But I want to cancel the download if it takes longer than 5 minutes. How can I modify the code below for that?
def download(url):
# request below can take several minutes
request.get(url)
threads = []
for url in [url1, url2, ...]:
thread = threading.Thread(target=download, args=url)
thread.start()
threads.append(thread)
for thread in threads:
thread.join(300)
if thread.is_alive():
# need to kill the thread here