chore:use threadPool
This commit is contained in:
parent
5f75c45779
commit
32f1625d44
21
main.py
21
main.py
@ -6,6 +6,8 @@ import os
|
||||
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 @@ class GetSource():
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user