Skip to content

Commit

Permalink
chore:use threadPool
Browse files Browse the repository at this point in the history
  • Loading branch information
Guovin committed Feb 6, 2024
1 parent 5f75c45 commit 32f1625
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 32f1625

Please sign in to comment.