import multiprocessing


def get_num_parallel_workers(num_parallel_workers):
    """
    Get num_parallel_workers used in dataset operations.
    If num_parallel_workers > the real CPU cores number, set num_parallel_workers = the real CPU cores number.
    """
    cores = multiprocessing.cpu_count()
    if isinstance(num_parallel_workers, int):
        if cores < num_parallel_workers:
            print(f"The num_parallel_workers {num_parallel_workers} is set too large, now set it {cores}")
            num_parallel_workers = cores
    else:
        print(f"The num_parallel_workers {num_parallel_workers} is invalid, now set it {min(cores, 8)}")
        num_parallel_workers = min(cores, 8)
    return num_parallel_workers