Reading system resource information

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
Decepto
User
Beiträge: 2
Registriert: Dienstag 15. Juli 2008, 06:08

Hallo von Denver. Erbärmlich für aufstellen auf Englisch. Mein Deutsch ist sehr schlecht. :cry:

Hi. There is a common Unix program called "top" that outputs useful system information like "load average", "memory usage", "cpu usage", etc.

I know I can do the following and parse the data within a python script...

Code: Alles auswählen

import os
topData = os.system("top")
# the 'free' command lists free memory
# freeData = os.system("free")
But, I'd rather not rely on the "top" command. Is there a way to view the amount of free memory in a system using pure python?
Benutzeravatar
veers
User
Beiträge: 1219
Registriert: Mittwoch 28. Februar 2007, 20:01
Wohnort: Zürich (CH)
Kontaktdaten:

Hi Decepto,

As you've noticed this is basically a German forum. Anyway... that useful information is located in the /proc folder. Check /proc/uptime, /proc/meminfo and /proc/loadavg. There is a folder for each process in /proc as well. You can find even more information in the /sys folder.

I hope that helps,

- Jonas
[url=http://29a.ch/]My Website - 29a.ch[/url]
"If privacy is outlawed, only outlaws will have privacy." - Phil Zimmermann
BlackJack

For the load average there's `os.getloadavg()` and for the times of the running Python program itself `os.times()`.

For the rest I don't know of a cross platform way, except trying to parse the output of the common tools like `top` and `free`. If you restrict yourself to Linux you maybe can parse `/proc/meminfo` and the `/proc/$PID/` files like `stat`, `statm`, or `status`.
lunar

Decepto hat geschrieben:Hallo von Denver. Erbärmlich für aufstellen auf Englisch. Mein Deutsch ist sehr schlecht. :cry:
Doesn't matter ... btw, english support mostly happens in comp.lang.python which is a very active, high quality newsgroup.
But, I'd rather not rely on the "top" command. Is there a way to view the amount of free memory in a system using pure python?
If you're only interested in Linux-compatibility, there's the /proc filesystem (see "man 5 proc"), if unix-compatibility is required, you have to rely on posix commands like "top", since there is no standardized, cross-plattform POSIX API to query system information.
Decepto
User
Beiträge: 2
Registriert: Dienstag 15. Juli 2008, 06:08

Thank you all very very much. That was exactly what I was looking for! :D
Antworten