Spaces:
Running
Running
AI Bot commited on
Commit ·
9c8e3ad
1
Parent(s): acde457
Optimize connection pool and concurrency to prevent target server crashes
Browse files- src/crawler.py +7 -3
src/crawler.py
CHANGED
|
@@ -21,10 +21,14 @@ class Crawler:
|
|
| 21 |
# impersonate="chrome120" is key. verify=False to speed up SSL handshakes.
|
| 22 |
self.session = requests.Session(impersonate="chrome120", verify=False)
|
| 23 |
self.ua = UserAgent()
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
self.scraper = cloudscraper.create_scraper(
|
| 26 |
browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True}
|
| 27 |
)
|
|
|
|
|
|
|
| 28 |
|
| 29 |
# Concurrency control for Playwright (heavy resource)
|
| 30 |
self.playwright_semaphore = threading.Semaphore(3)
|
|
@@ -342,8 +346,8 @@ class Crawler:
|
|
| 342 |
logger.info(f"Starting crawl for domain: {base_domain}")
|
| 343 |
|
| 344 |
# Use ThreadPoolExecutor for parallel crawling with high concurrency
|
| 345 |
-
#
|
| 346 |
-
with concurrent.futures.ThreadPoolExecutor(max_workers=
|
| 347 |
# Map of future -> url
|
| 348 |
future_to_url = {}
|
| 349 |
|
|
|
|
| 21 |
# impersonate="chrome120" is key. verify=False to speed up SSL handshakes.
|
| 22 |
self.session = requests.Session(impersonate="chrome120", verify=False)
|
| 23 |
self.ua = UserAgent()
|
| 24 |
+
# Increase connection pool sizes to handle concurrency without warnings
|
| 25 |
+
import requests as std_requests
|
| 26 |
+
adapter = std_requests.adapters.HTTPAdapter(pool_connections=50, pool_maxsize=50)
|
| 27 |
self.scraper = cloudscraper.create_scraper(
|
| 28 |
browser={'browser': 'chrome', 'platform': 'windows', 'desktop': True}
|
| 29 |
)
|
| 30 |
+
self.scraper.mount('http://', adapter)
|
| 31 |
+
self.scraper.mount('https://', adapter)
|
| 32 |
|
| 33 |
# Concurrency control for Playwright (heavy resource)
|
| 34 |
self.playwright_semaphore = threading.Semaphore(3)
|
|
|
|
| 346 |
logger.info(f"Starting crawl for domain: {base_domain}")
|
| 347 |
|
| 348 |
# Use ThreadPoolExecutor for parallel crawling with high concurrency
|
| 349 |
+
# Sweet spot is 30. Any higher causes the target server (bestcheck.in) to drop SSL connections (SSLEOFError)
|
| 350 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=30) as executor:
|
| 351 |
# Map of future -> url
|
| 352 |
future_to_url = {}
|
| 353 |
|