File size: 587 Bytes
8b0f105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import concurrent.futures

_executor = None

def get_executor() -> concurrent.futures.ProcessPoolExecutor:
    """Lazily initialize and return a shared ProcessPoolExecutor."""
    global _executor
    if _executor is None:
        # Use 2 workers to avoid CPU/RAM overhead on lightweight instances
        _executor = concurrent.futures.ProcessPoolExecutor(max_workers=2)
    return _executor

def shutdown_executor():
    """Shut down the ProcessPoolExecutor cleanly."""
    global _executor
    if _executor is not None:
        _executor.shutdown(wait=False)
        _executor = None