卢克是正确的。为了获得 Processor(_Total)\% Processor Time 的本地化版本,我们必须获取路径 "Processor" 和 "%Processor Time" 的每个组件的本地化名称,其中 '(_Total)' 是常数. 索引可能因操作系统版本而异,因此您必须在每次运行时发现它们。win32pyutil模块包含将加载英语到索引映射但保留它的方法,并且如果您只需要它一次它并不小,那么这可能是内存浪费。我们使用以下内容:
def _find_pdh_counter_localized_name(eng_names,machine_name=None):
'''
Create a map of english names to indexes. We then lookup the english
name in the map to get the localized name.
Shamefully lifted from win32pdhutil, only this one uses a transient map
instead of a persistent one.
Will throw KeyError if a name is asked for that is not in the list.
'''
import win32api, win32con
counter_reg_value = win32api.RegQueryValueEx(
win32con.HKEY_PERFORMANCE_DATA, "Counter 009"
)
counter_list = counter_reg_value[0]
eng_map={}
for i in range(0, len(counter_list) - 1, 2):
try:
counter_id = int(counter_list[i])
except ValueError:
continue
eng_map[counter_list[i+1].lower()] = counter_id
ret = []
for name in eng_names:
ret.append(win32pdh.LookupPerfNameByIndex(
machine_name, eng_map[name.lower()])
)
del eng_map
return tuple(ret)
构造计数器名称:
names = _find_pdh_counter_localized_name(['processor','% processor time'])
counter_name = r'\%s(_Total)\%s' % names
这会产生所需的值。例如,意大利语中的“\Processore(_Total)\% Tempo Processore”。