Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

python - Accessing CPU/RAM usage (like with Task Manager, but via API!)?

Is there a specific way to access "task manager" information with the Windows API? I have done a fair bit of searching on the matter, but I can't seem to find an API call that will tell me either:

  • A given process's CPU/RAM usage
  • The process which is using the most CPU/RAM

Is there a way to access that information via Python or C++ (basically, via the Windows API)?

Here's what I'm essentially trying to do (in pseudo code):

app x = winapi.most_intensive_process
app y = winapi.most_RAM_usage

print x.name
print y.name
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can retrieve information about RAM usage with the PSAPI functions, especially EnumProcesses to find all the processes in the system, and GetProcessMemoryInfo to get information about each process' memory usage.

You can retrieve CPU usage for each process with GetProcessTimes. This isn't always perfectly accurate, but I believe the Task Manager produces results that are inaccurate in the same way under the same circumstances.

In case you want it, information about memory usage by the system as a whole is available via GetPerformanceInfo.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...