Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

refactor: Convert ParallelSearchEngine to use global thread pool

LearningCircuit91fe4feab437
diff --git a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
index dc88ccb37b..44acc0a363 100644
--- a/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
+++ b/src/local_deep_research/web_search_engines/engines/parallel_search_engine.py
@@ -3,6 +3,7 @@ import os
import concurrent.futures
from typing import Any, Dict, List, Optional
from threading import Lock
+import atexit
from loguru import logger
@@ -13,21 +14,92 @@ from ...web.services.socket_service import SocketIOService
from ..search_engine_base import BaseSearchEngine
from ..search_engine_factory import create_search_engine
+# Global thread pool shared by all ParallelSearchEngine instances
+# This prevents creating multiple thread pools and having more threads than expected
+_global_executor: Optional[concurrent.futures.ThreadPoolExecutor] = None
+_global_executor_lock = Lock()
+
+
+def _get_global_executor(
+ max_workers: Optional[int] = None,
+) -> Optional[concurrent.futures.ThreadPoolExecutor]:
+ """
+ Get or initialize the global thread pool executor.
+ Thread-safe lazy initialization ensures only one pool is created.
+
+ Args:
+ max_workers: Number of worker threads. If None, uses Python's recommended
+ formula: min(32, (os.cpu_count() or 1) + 4) for I/O-bound operations.
+ Only used on first initialization; subsequent calls ignore this parameter.
+
+ Returns:
+ The global ThreadPoolExecutor instance, or None if initialization fails
+ """
+ global _global_executor
+
+ with _global_executor_lock:
+ if _global_executor is None:
+ if max_workers is None:
+ max_workers = min(32, (os.cpu_count() or 1) + 4)
+
+ try:
+ _global_executor = concurrent.futures.ThreadPoolExecutor(
+ max_workers=max_workers,
+ thread_name_prefix="parallel_search_",
+ )
+ logger.info(
+ f"Initialized global ThreadPool with {max_workers} workers "
+ f"(shared by all ParallelSearchEngine instances)"
+ )
+ except Exception:
+ logger.exception(
+ "Failed to create global ThreadPoolExecutor, parallel search will not work"
+ )
+ return None
+
+ return _global_executor
+
+
+def shutdown_global_executor(wait: bool = True):
+ """
+ Shutdown the global thread pool executor.
+
+ This is called automatically at process exit via atexit.
+ After calling this, any new ParallelSearchEngine instances will create a new pool.
+
+ Args:
+ wait: If True, wait for all threads to complete before returning
+ """
+ global _global_executor
+
+ with _global_executor_lock:
+ if _global_executor is not None:
+ try:
+ _global_executor.shutdown(wait=wait)
+ logger.info("Global ThreadPool shutdown complete")
+ except Exception:
+ logger.exception("Error shutting down global ThreadPool")
+ finally:
+ _global_executor = None
+
+
+# Register automatic cleanup at process exit
+atexit.register(lambda: shutdown_global_executor(wait=True))
+
class ParallelSearchEngine(BaseSearchEngine):
"""
Parallel search engine that executes multiple search engines simultaneously.
Uses LLM to select appropriate engines based on query, then runs them all in parallel.
+ Thread Pool Management:
+ All instances share a single global thread pool to prevent thread proliferation.
+ The pool is automatically cleaned up at process exit.
+
Usage:
- Recommended: Use as context manager to ensure proper cleanup:
- with ParallelSearchEngine(llm) as engine:
- results = engine.run("query")
-
- Alternative: Manually call shutdown() when done:
- engine = ParallelSearchEngine(llm)
- results = engine.run("query")
- engine.shutdown() # Important: cleanup thread pool
+ engine = ParallelSearchEngine(llm)
+ results = engine.run("query")
+ # No manual cleanup needed - global pool is shared and cleaned up automatically
"""
def __init__(
@@ -50,13 +122,9 @@ class ParallelSearchEngine(BaseSearchEngine):
"""
Initialize the parallel search engine.
- Important: This class should be used as a context manager or have shutdown()
- called explicitly to ensure proper thread pool cleanup.
-
- Example:
- with ParallelSearchEngine(llm) as engine:
- results = engine.run("query")
- # Thread pool automatically cleaned up
+ All instances share a global thread pool. The first instance created will
+ initialize the pool with the specified max_workers (or auto-detected value).
+ Subsequent instances reuse the existing pool.
Args:
llm: Language model instance for query classification
@@ -69,8 +137,9 @@ class ParallelSearchEngine(BaseSearchEngine):
max_filtered_results: Maximum number of results to keep after filtering
settings_snapshot: Settings snapshot for thread context
programmatic_mode: If True, disables database operations and metrics tracking
- max_workers: Thread pool size. If None, uses Python's recommended formula:
- min(32, (os.cpu_count() or 1) + 4) for I/O-bound operations
+ max_workers: Thread pool size for the FIRST instance only. If None, uses Python's
+ recommended formula: min(32, (os.cpu_count() or 1) + 4) for I/O-bound operations.
+ Ignored if the global pool is already initialized.
**kwargs: Additional parameters (ignored but accepted for compatibility)
"""
# Override max_filtered_results to be much higher for parallel search
@@ -107,27 +176,9 @@ class ParallelSearchEngine(BaseSearchEngine):
self.engine_cache = {}
self.cache_lock = Lock()
- # Persistent thread pool for parallel searches (performance optimization)
- # Reusing threads avoids repeated thread creation/destruction overhead
- # Use Python's recommended formula for I/O-bound operations if not specified
- if max_workers is None:
- self.max_workers = min(32, (os.cpu_count() or 1) + 4)
- else:
- self.max_workers = max_workers
-
- try:
- self._executor = concurrent.futures.ThreadPoolExecutor(
- max_workers=self.max_workers,
- thread_name_prefix="parallel_search_",
- )
- logger.debug(
- f"Initialized persistent ThreadPool with {self.max_workers} workers"
- )
- except Exception:
- logger.exception(
- "Failed to create ThreadPoolExecutor, parallel search will not work"
- )
- self._executor = None
+ # Initialize global thread pool (thread-safe, only happens once)
+ # All instances share the same pool to prevent thread proliferation
+ _get_global_executor(max_workers)
# Get available engines (excluding 'meta', 'auto', and 'parallel')
self.available_engines = self._get_available_engines()
@@ -546,16 +597,17 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu
all_results = []
engine_results = {}
- # Check if thread pool is available
- if self._executor is None:
+ # Get the global thread pool
+ executor = _get_global_executor()
+ if executor is None:
logger.error(
- "Thread pool not available, cannot execute parallel search"
+ "Global thread pool not available, cannot execute parallel search"
)
return []
- # Submit all tasks to the persistent executor
+ # Submit all tasks to the global executor
future_to_engine = {
- self._executor.submit(
+ executor.submit(
self._execute_single_engine, engine_name, query
): engine_name
for engine_name in selected_engines
@@ -702,29 +754,5 @@ Select up to {self.max_engines_to_select} engines that would best answer this qu
"""Compatibility method for LangChain tools"""
return self.run(query)
- def shutdown(self, wait: bool = True):
- """
- Shutdown the persistent thread pool.
-
- Should be called when the search engine is no longer needed to properly
- cleanup resources and ensure all pending tasks complete.
-
- Args:
- wait: If True, wait for all pending tasks to complete before shutdown
- """
- if self._executor is not None:
- logger.info("Shutting down persistent ThreadPool")
- try:
- self._executor.shutdown(wait=wait)
- logger.info("ThreadPool shutdown complete")
- except Exception:
- logger.exception("Error during ThreadPool shutdown")
-
- def __enter__(self):
- """Support using as context manager."""
- return self
-
- def __exit__(self, exc_type, exc_val, exc_tb):
- """Cleanup on context manager exit."""
- self.shutdown(wait=True)
- return False
+ # Note: No shutdown() or context manager methods needed
+ # The global thread pool is automatically cleaned up at process exit via atexit