获取windows server 的cpu和gpu个数

获取cpu的个数

     import os
     def get_cpus():
        if 'NUMBER_OF_PROCESSORS' in os.environ:
            cpu_num = os.environ['NUMBER_OF_PROCESSORS']
            return cpu_num
        return 0

获取gpu的个数

    nvidia_path = "C:\\Program Files\\NVIDIA Corporation\\NVSMI\\nvidia-smi.exe"
    def get_gpus(nvidia_path):
        if not os.path.exists(nvidia_path):
            logging.error("Can not find nvidia smi exe, please check the path.")
            return 0
        cmd = '"{0}" -L'.format(nvidia_path)
        gpus = 0
        try:
            with os.popen(cmd) as fd:
                for line in fd:
                    if 'GPU' in line:
                        gpus += 1
        except Exception as e:
            logging.error("Get gpu count failed with error {0}".format(e))
        return gpus

这里包含了带空格的cmd命令怎么处理, 带空格的路径先用“” 引起来, 然后结合参数再在外面包一层单引号.

你可能感兴趣的:(获取windows server 的cpu和gpu个数)