diff --git a/main.py b/main.py index 39e47e2423..267b383f7f 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,8 @@ import re import requests from selenium_stealth import stealth +import concurrent.futures +from concurrent.futures import ThreadPoolExecutor class GetSource(): source_file = 'demo.txt' @@ -67,20 +69,17 @@ def getSpeed(self,url): except: print('request timeout or error') end = time.time() - return end - start + return url, end - start # 返回 URL 和响应时间 def compareSpeed(self,pageUrls): response_times = [] - for pageUrl in pageUrls: - response_times.append(self.getSpeed(pageUrl)) - - sorted_urls = zip(pageUrls, response_times) - sorted_urls = sorted(sorted_urls, key=lambda x: x[1]) - - pageUrls_new =[] - for url, _ in sorted_urls: - pageUrls_new.append(url) - + with ThreadPoolExecutor(max_workers=20) as executor: # 创建一个线程池,最大并发数为 20 + future_to_url = {executor.submit(self.getSpeed, url): url for url in pageUrls} + for future in concurrent.futures.as_completed(future_to_url): + url, response_time = future.result() + response_times.append((url, response_time)) + sorted_urls = sorted(response_times, key=lambda x: x[1]) + pageUrls_new = [url for url, _ in sorted_urls] return pageUrls_new def removeFile(self):