Asked  7 Months ago    Answers:  5   Viewed   42 times

What's your preferred way of getting current system status (current CPU, RAM, free disk space, etc.) in Python? Bonus points for *nix and Windows platforms.

There seems to be a few possible ways of extracting that from my search:

  1. Using a library such as PSI (that currently seems not actively developed and not supported on multiple platform) or something like pystatgrab (again no activity since 2007 it seems and no support for Windows).

  2. Using platform specific code such as using a os.popen("ps") or similar for the *nix systems and MEMORYSTATUS in ctypes.windll.kernel32 (see this recipe on ActiveState) for the Windows platform. One could put a Python class together with all those code snippets.

It's not that those methods are bad but is there already a well-supported, multi-platform way of doing the same thing?



The psutil library gives you information about CPU, RAM, etc., on a variety of platforms:

psutil is a module providing an interface for retrieving information on running processes and system utilization (CPU, memory) in a portable way by using Python, implementing many functionalities offered by tools like ps, top and Windows task manager.

It currently supports Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures, with Python versions from 2.6 to 3.5 (users of Python 2.4 and 2.5 may use 2.1.3 version).

Some examples:

#!/usr/bin/env python
import psutil
# gives a single float value
# gives an object with many fields
# you can convert that object to a dictionary 
# you can have the percentage of used RAM
# you can calculate percentage of available memory
psutil.virtual_memory().available * 100 / psutil.virtual_memory().total

Here's other documentation that provides more concepts and interest concepts:

Tuesday, June 1, 2021
answered 7 Months ago

Use PHPSysInfo library

phpSysInfo is a open source PHP script that displays information about the host being accessed. It will displays things like:

  • Uptime
  • CPU
  • Memory
  • Ethernet
  • Floppy
  • Video Information

It directly parsed parses /proc and does not use exec.

Another way is to use Linfo. It is a very fast cross-platform php script that describes the host server in extreme detail, giving information such as ram usage, disk space, raid arrays, hardware, network cards, kernel, os, samba/cups/truecrypt status, temps, disks, and much more.

Wednesday, March 31, 2021
answered 9 Months ago

There is an open source library that gives these (and more system info stuff) across many platforms: SIGAR API

I've used it in fairly large projects and it works fine (except for certain corner cases on OS X etc.)

Wednesday, June 23, 2021
answered 6 Months ago
>>> import os
>>> os.times()
(1.296875, 0.765625, 0.0, 0.0, 0.0)
>>> print os.times.__doc__
times() -> (utime, stime, cutime, cstime, elapsed_time)

Return a tuple of floating point numbers indicating process times.

From the (2.5) manual:

times( )

Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children's user time, children's system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. Availability: Macintosh, Unix, Windows.

Wednesday, July 28, 2021
answered 5 Months ago

On Linux systems multiprocessing.cpu_count() relies on a sysconf (_SC_NPROCESSORS_ONLN) call, which returns the number of online CPUs in contrast to sysconf (_SC_NPROCESSORS_CONF) which returns the number of configured CPUs.

The values might differ in systems with advanced CPU power management functionality that sets CPU cores offline to save energy or with similar dynamic CPU activation functionality.

Tuesday, August 3, 2021
Sergio Carneiro
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :