-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Feat/docker PDF strategy #1518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/docker PDF strategy #1518
Changes from all commits
9c6e016
057fb61
5adc9dc
d231d61
c709082
481e7fe
726f41a
47bd392
e17484f
94de653
bc3b3d1
44b280b
21f80ff
1636383
056721a
ec2f88a
9681567
87f35b8
2b80f98
703243d
14994d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| # crawler_pool.py (new file) | ||
| import asyncio, json, hashlib, time, psutil | ||
| from contextlib import suppress | ||
| from typing import Dict | ||
| from typing import Dict, Optional | ||
| from crawl4ai import AsyncWebCrawler, BrowserConfig | ||
| from typing import Dict | ||
| from utils import load_config | ||
|
|
||
|
|
||
| CONFIG = load_config() | ||
|
|
||
| POOL: Dict[str, AsyncWebCrawler] = {} | ||
|
|
@@ -15,20 +15,33 @@ | |
| MEM_LIMIT = CONFIG.get("crawler", {}).get("memory_threshold_percent", 95.0) # % RAM – refuse new browsers above this | ||
| IDLE_TTL = CONFIG.get("crawler", {}).get("pool", {}).get("idle_ttl_sec", 1800) # close if unused for 30 min | ||
|
|
||
| def _sig(cfg: BrowserConfig) -> str: | ||
| payload = json.dumps(cfg.to_dict(), sort_keys=True, separators=(",",":")) | ||
| return hashlib.sha1(payload.encode()).hexdigest() | ||
| def _sig(cfg: BrowserConfig, crawler_strategy: Optional[object] = None) -> str: | ||
| """ | ||
| Generate a unique signature for a crawler based on browser config | ||
| and optional crawler strategy. This ensures that crawlers with | ||
| different strategies (e.g., PDF) are stored separately in the pool. | ||
| """ | ||
| payload = cfg.to_dict() | ||
|
|
||
| if crawler_strategy is not None: | ||
| payload["strategy"] = crawler_strategy.__class__.__name__ | ||
|
|
||
|
Comment on lines
+26
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include strategy configuration in the pool signature. Keying pooled crawlers only by 🤖 Prompt for AI Agents |
||
| json_payload = json.dumps(payload, sort_keys=True, separators=(",", ":")) | ||
| return hashlib.sha256(json_payload.encode()).hexdigest() | ||
|
|
||
|
|
||
|
|
||
| async def get_crawler(cfg: BrowserConfig) -> AsyncWebCrawler: | ||
| async def get_crawler(cfg: BrowserConfig, crawler_strategy: Optional[object] = None) -> AsyncWebCrawler: | ||
| sig: Optional[str] = None | ||
| try: | ||
| sig = _sig(cfg) | ||
| sig = _sig(cfg, crawler_strategy=crawler_strategy) | ||
| async with LOCK: | ||
| if sig in POOL: | ||
| LAST_USED[sig] = time.time(); | ||
| return POOL[sig] | ||
| if psutil.virtual_memory().percent >= MEM_LIMIT: | ||
| raise MemoryError("RAM pressure – new browser denied") | ||
| crawler = AsyncWebCrawler(config=cfg, thread_safe=False) | ||
| crawler = AsyncWebCrawler(config=cfg, thread_safe=False, crawler_strategy=crawler_strategy) | ||
| await crawler.start() | ||
| POOL[sig] = crawler; LAST_USED[sig] = time.time() | ||
| return crawler | ||
|
|
@@ -37,13 +50,15 @@ async def get_crawler(cfg: BrowserConfig) -> AsyncWebCrawler: | |
| except Exception as e: | ||
| raise RuntimeError(f"Failed to start browser: {e}") | ||
| finally: | ||
| if sig in POOL: | ||
| if sig and sig in POOL: | ||
| LAST_USED[sig] = time.time() | ||
| else: | ||
| # If we failed to start the browser, we should remove it from the pool | ||
| POOL.pop(sig, None) | ||
| LAST_USED.pop(sig, None) | ||
| if sig: | ||
| POOL.pop(sig, None) | ||
| LAST_USED.pop(sig, None) | ||
| # If we failed to start the browser, we should remove it from the pool | ||
|
|
||
| async def close_all(): | ||
| async with LOCK: | ||
| await asyncio.gather(*(c.close() for c in POOL.values()), return_exceptions=True) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.