File size: 467 Bytes
8da7bdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import os
import cv2
import torch

def configure_threads_for_inference(num_threads: int | None = 2) -> None:
    """
    Giới hạn số luồng hoạt động của OpenCV và PyTorch tránh tranh chấp CPU.
    """
    if num_threads is None:
        num_threads = max(1, (os.cpu_count() or 2) // 2)
    cv2.setNumThreads(num_threads)
    torch.set_num_threads(num_threads)
    try:
        torch.set_num_interop_threads(1)
    except RuntimeError:
        pass