python获取windows进程cpu占用率_用Python计算进程cpu使用率

经过进一步的研究,我找到了解决办法。在

因此,为了获得进程cpu使用率的百分比,我们需要一些参数:

1。系统时间

为了计算这个,我们需要用户模式时间,内核模式时间和

空闲模式时间:from ctypes import *

import time

class FILETIME(Structure):

_fields_ = [

("dwLowDateTime", DWORD),

("dwHighDateTime", DWORD)]

def GetSystemTimes():

"""

Uses the function GetSystemTimes() (win32) in order to get the user mode time, kernel mode time and idle mode time

:return: user time, kernel time and idle time (Dictinary)

"""

__GetSystemTimes = windll.kernel32.GetSystemTimes

idleTime, kernelTime, userTime = FILETIME(), FILETIME(), FILETIME()

success = __GetSystemTimes(

byref(idleTime),

byref(kernelTime),

byref(userTime))

assert success, ctypes.WinError(ctypes.GetLastError()

你可能感兴趣的:(python获取windows进程cpu占用率_用Python计算进程cpu使用率)