Is there a way to monitor the amount of processing power used in a program in Python? My goal is to kill a program if someone is using too much power while connecting to my server in case of malicious intent.
3 Answers
Even if psutil provides OS-level information in a relatively OS-independent way, sometimes it is better indicated to interact directly with the local OS. Doing so at the API level can be seen as too complex and difficult to debug. If this is the case, some useful alternatives in the case of Windows are:
wmic process list full
and
wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime
That's the closest Windows equivalent to *nix's better known ps.
The suggestion is, then, to "Popen" them, and pipe their output back for analysis. My understanding is that at least the first (wmic process) supports continuous mode.
Comments
>>> for x in range(3):
... psutil.cpu_percent(interval=1, percpu=True)
...
[4.0, 6.9]
[7.0, 8.5]
[1.2, 9.0]
ulimitfrom the shell before launching the Python interpreter.