File size: 2,107 Bytes
17f1739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
import threading
from app.utils.config import settings

logger = logging.getLogger(__name__)

# Singleton instance and lock
_analyzer_instance = None
_initialization_lock = threading.Lock()
_is_initializing = False

def get_analyzer_instance():
    """
    Get the global analyzer instance (Lazy loading).
    Note: If not ready, this will still trigger a synchronous load if called from a route.
    Best used after background_init_analyzer has completed.
    """
    global _analyzer_instance
    if _analyzer_instance is None:
        with _initialization_lock:
            # Double-check inside lock
            if _analyzer_instance is None:
                logger.info("πŸš€ Initializing ProductionCertificateAnalyzer (blocking load)...")
                from app.analyzers.certificate_analyzer import ProductionCertificateAnalyzer
                _analyzer_instance = ProductionCertificateAnalyzer(use_ml=settings.use_ml)
                logger.info("βœ… Analyzer instance initialized successfully")
    return _analyzer_instance

def init_analyzer_background():
    """
    Initializes the analyzer in a background thread to avoid blocking the event loop.
    """
    global _analyzer_instance, _is_initializing
    
    if _analyzer_instance is not None or _is_initializing:
        return
        
    def _target():
        global _analyzer_instance, _is_initializing
        with _initialization_lock:
            try:
                _is_initializing = True
                logger.info("🧡 Threaded initialization of ProductionCertificateAnalyzer started...")
                from app.analyzers.certificate_analyzer import ProductionCertificateAnalyzer
                _analyzer_instance = ProductionCertificateAnalyzer(use_ml=settings.use_ml)
                logger.info("βœ… Threaded initialization complete")
            except Exception as e:
                logger.error(f"❌ Threaded initialization failed: {e}")
            finally:
                _is_initializing = False
                
    thread = threading.Thread(target=_target, daemon=True)
    thread.start()