Coverage for openhcs/io/async_init.py: 40.0%
18 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 02:09 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 02:09 +0000
1"""
2Async initialization for I/O backends.
4This module provides background loading of GPU-heavy dependencies
5to avoid blocking during import while ensuring they're ready when needed.
6"""
7import logging
8import threading
10logger = logging.getLogger(__name__)
12_init_complete = threading.Event()
13_init_thread = None
16def _background_init():
17 """
18 Background thread target for async initialization.
20 NOTE: Due to Python's GIL, importing heavy modules in a background thread
21 still blocks the main thread during imports. Therefore, we use lazy initialization
22 for GPU libraries and storage backends - they'll be loaded on first use.
24 Currently this is a no-op placeholder. GPU registry initialization happens
25 lazily on first use, not during startup.
26 """
27 try:
28 logger.info("Background I/O initialization complete (lazy mode - no-op)")
29 except Exception as e:
30 logger.error(f"Background I/O initialization failed: {e}")
31 finally:
32 _init_complete.set()
35def start_async_initialization():
36 """
37 Start background initialization of GPU-heavy dependencies.
39 Call this during application startup (GUI/CLI) to pre-load
40 GPU libraries without blocking. Safe to call multiple times.
41 """
42 global _init_thread
44 if _init_thread is None:
45 _init_thread = threading.Thread(
46 target=_background_init,
47 daemon=True,
48 name="io-async-init"
49 )
50 _init_thread.start()
51 logger.info("Started background I/O initialization")
54def wait_for_initialization(timeout: float = 60.0) -> bool:
55 """
56 Wait for background initialization to complete.
58 Args:
59 timeout: Maximum seconds to wait
61 Returns:
62 True if completed, False if timeout
63 """
64 return _init_complete.wait(timeout)