ルカは正しいです。Processor(_Total)\% Processor Time のローカライズされたバージョンを取得するには、「(_Total)」が定数であるパス「Processor」および「%Processor Time」の各コンポーネントのローカライズされた名前を取得する必要があります。 . インデックスは OS のバージョンによって異なる可能性があるため、実行ごとに検出する必要があります。win32pyutilモジュールには、english-to-index マップをロードするが保持するメソッドが含まれています。一度だけ必要な場合は小さくないため、メモリの浪費になる可能性があります。以下を使用します。
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」。