
python
import os
import concurrent.futures
import time
def download_file(url, filename):
response = requests.get(url)
with open(filename, 'wb') as f:
f.write(response.content)
def main():
url = "https://chromedriver.storage.googleapis.com/2.48/chromedriver_linux64.zip"
filename = "chromedriver.zip"
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(download_file, url, filename) for _ in range(10)]
for future in concurrent.futures.as_completed(futures):
try:
future.result()
print("下载完成")
except Exception as e:
print(f"下载失败: {e}")
if __name__ == "__main__":
main()
这个示例中,我们首先定义了一个`download_file`函数,用于下载文件。然后,在`main`函数中,我们使用`concurrent.futures.ThreadPoolExecutor`创建一个线程池,并提交10个任务到线程池中。每个任务都会尝试下载一个文件。当所有任务都完成后,我们打印出“下载完成”。



