简体   繁体   English

访问CPU / RAM使用率(与任务管理器一样,但通过API!)?

[英]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? 是否有使用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: 我对此事做了相当多的搜索,但似乎找不到能够告诉我的API调用:

  • A given process's CPU/RAM usage 给定进程的CPU / RAM使用率
  • The process which is using the most CPU/RAM 使用最多CPU / RAM的进程

Is there a way to access that information via Python or C++ (basically, via the Windows API)? 是否可以通过Python或C ++(基本上是通过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

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. 您可以使用PSAPI函数检索有关RAM使用情况的信息,尤其是EnumProcesses可以查找系统中的所有进程,而GetProcessMemoryInfo可以获取有关每个进程的内存使用情况的信息。

You can retrieve CPU usage for each process with GetProcessTimes . 您可以使用GetProcessTimes检索每个进程的CPU使用率。 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 . 如果需要,可以通过GetPerformanceInfo获得有关整个系统内存使用情况的信息。

Instead of calling the windows API directly you can use the psutil library which is a cross-platform library that provides a lot of information about processes. 可以直接使用psutil库(而不是直接调用Windows API),该库是一个跨平台的库,提供了许多有关进程的信息。 It works on Windows, Linux, Mac OS, BSD and Sun Solaris and works with python from 2.4 to 3.4 in both 32 and 64 bit fashion. 它可在Windows,Linux,Mac OS,BSD和Sun Solaris上运行,并以32位和64位方式与python从2.4到3.4一起工作。

In particular it's Process class has the following interesting methods: 特别是它的Process类具有以下有趣的方法:

  • cpu_times : user and system timings spent by the process from its start. cpu_times :该进程从开始就花费的用户和系统计时。
  • cpu_percent : percentage of cpu utilization since last call or in the given interval cpu_percent :自上次调用以来或在给定间隔内cpu利用率的百分比
  • memory_info : info about Ram and virtual memory usage. memory_info :有关Ram和虚拟内存使用情况的信息。 NOTE : the documentation explicitly states that these are the one shown by taskmgr.exe so it looks like exactly what you want. 注意 :该文档明确指出这些是taskmgr.exe显示的内容,因此它看起来确实正是您想要的。
  • memory_info_ex : extended memory information. memory_info_ex :扩展的内存信息。
  • memory_percent : percentage of used memory by the process. memory_percent :进程使用的内存百分比。

To iterate over all processes (in order to find the most CPU/memory hungry for example), you can just iterate over process_iter . 要遍历所有进程(例如,查找最繁忙的CPU /内存),只需遍历process_iter


Here's a simple implementation of what you wanted to achieve: 这是您要实现的目标的简单实现:

import psutil

def most_intensive_process():
    return max(psutil.process_iter(), key=lambda x: x.cpu_percent(0))

def most_RAM_usage():
    return max(psutil.process_iter(), key=lambda x: x.memory_info()[0])

x = most_intensive_process()
y = most_RAM_usage()

print(x.name)
print(y.name)

Sample run on my system: 示例在我的系统上运行:

In [23]: def most_intensive_process():
    ...:     # psutil < 2.x has get_something style methods...
    ...:     return max(psutil.process_iter(), key=lambda x: x.get_cpu_percent(0))
    ...: 
    ...: def most_RAM_usage():
    ...:     return max(psutil.process_iter(), key=lambda x: x.get_memory_info()[0])

In [24]: x = most_intensive_process()
    ...: y = most_RAM_usage()
    ...: 

In [25]: print(x.name, y.name)
firefox firefox

You can use the following Windows API to retrieve various process counters in C/C++ program . 您可以使用以下Windows API来检索C / C ++程序中的各种进程计数器。

It retrieves information about the memory usage of the specified process. 它检索有关指定进程的内存使用情况的信息。

BOOL WINAPI GetProcessMemoryInfo(
  _In_   HANDLE Process,
  _Out_  PPROCESS_MEMORY_COUNTERS ppsmemCounters,
  _In_   DWORD cb
);

There is complete example on MSDN, which explains how it can be used to retrieve such information for your process. MSDN上有完整的示例,该示例说明了如何使用它为您的过程检索此类信息。

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682050%28v=vs.85%29.aspx http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms682050%28v=vs.85%29.aspx

You have mentioned that you would like to fetch the information on continuous basis(with some time interval as task manager do). 您已经提到过,您想连续获取信息(任务管理器需要一定的时间间隔)。 To achieve this you may want to write the complete logic(mentioned in MSDN) inside a function and call it after some time delay( Sleep(1second)) .This way you should be able to fetch all these information of your program till it executes. 为此,您可能需要在函数中编写完整的逻辑(在MSDN中提到),并在一定的时间延迟( Sleep(1second))后调用它。这样,您应该能够提取程序的所有这些信息,直到执行为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python CPU 和 RAM 内存使用情况 - Python CPU and RAM memory usage 获取Linux中进程的最大CPU / RAM使用率 - Getting the max CPU / RAM usage for a process in Linux 如何在没有 psutil 的情况下获取 CPU 和 RAM 使用率 - How to get CPU and RAM usage without psutil 如何在 Python 中获取当前的 CPU 和 RAM 使用情况? - How to get current CPU and RAM usage in Python? 可以使用python脚本(或任何其他编程语言脚本)来限制其他应用程序使用的资源(如RAM,CPU使用情况)吗? - Can python script (Or any other programming language scripts) be used to restrict resources (like RAM, CPU Usage) used by other applications? 如何在Python中获取特定程序的当前CPU,GPU和RAM使用情况? - How to get current CPU, GPU and RAM usage of a particular program in Python? 如何使此任务提高 CPU 使用率? - How to make this task improve cpu usage? 创建一个占用特定数量CPU和RAM的任务 - Create a task that takes up a specific amount of CPU and RAM 访问附加到特征类的数据时使用多个cpu - multiple cpu usage when accessing data attached to traited classes 在反应应用程序上显示服务器 RAM、CPU 和 GPU 使用情况的正确方法 - Proper way to show server's RAM, CPU and GPU usage on react app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM