python 调用powershell 命令获得GPU利用率
获取GPU使用率通过powershell, 输入get-counter -counter “\GPU Engine()\utilization percentage"
根据你使用的哪个软件的PID ,来获得这个程序的GPU使用率。
但是使用subprocees.Popen(cmd) , cmd = get-counter -counter "\GPU Engine()\utilization percentage” 会报错误,如term "*’ is not recognized as the name of a cmdlet.等错误。
最后查出是编码问题, python 3默认的编码是UTF8 ,而win10 cmd 默认的编码为437(OEM-United States), 中文win10是GBK.
所以命令行传入时需要加命令进行编码,
def get_gpu_video_codec0_utilization(self):
"""
# this utilization will need adjustment according to OS version
step1 : Get the powerdvd pip
step2: powershell cmd to get the GPU usage
"""
resp = subprocess.check_output('tasklist | findstr "PowerDVD.exe"', shell=True)
powerdvd_pid = resp.decode().partition("PowerDVD.exe")[2].strip().split(" ")[0]
print(powerdvd_pid)