有什麼辦法可以在python中獲得系統狀態,例如內存空間量,正在運行的進程,cpu負載等等。 我知道在Linux上我可以從/ proc目錄得到這個,但我想在unix和windows上做到這一點。在python中獲取系統狀態
10
A
回答
8
我不知道任何這樣的庫/包目前支持Linux和Windows。有libstatgrab這似乎不是非常積極的開發(它已經支持各種各樣的Unix平臺,但非常活躍的PSI (Python System Information))在AIX,Linux,SunOS和達爾文工作。這兩個項目的目標都是在未來某個時候支持Windows。祝你好運。
7
我不認爲這是應該是一個跨平臺的庫,但(那裏,這絕對是一個雖然)
我不過爲您提供一個片段我用來從/proc/stat
在當前CPU的負載Linux操作系統:
編輯:更換可怕的無證代碼稍微更Python和記錄代碼
import time
INTERVAL = 0.1
def getTimeList():
"""
Fetches a list of time units the cpu has spent in various modes
Detailed explanation at http://www.linuxhowtos.org/System/procstat.htm
"""
cpuStats = file("/proc/stat", "r").readline()
columns = cpuStats.replace("cpu", "").split(" ")
return map(int, filter(None, columns))
def deltaTime(interval):
"""
Returns the difference of the cpu statistics returned by getTimeList
that occurred in the given time delta
"""
timeList1 = getTimeList()
time.sleep(interval)
timeList2 = getTimeList()
return [(t2-t1) for t1, t2 in zip(timeList1, timeList2)]
def getCpuLoad():
"""
Returns the cpu load as a value from the interval [0.0, 1.0]
"""
dt = list(deltaTime(INTERVAL))
idle_time = float(dt[3])
total_time = sum(dt)
load = 1-(idle_time/total_time)
return load
while True:
print "CPU usage=%.2f%%" % (getCpuLoad()*100.0)
time.sleep(0.1)
+5
[os.getloadavg()](http://docs.python.org /library/os.html#os.getloadavg) – 2011-10-01 11:13:37
6
https://pypi.python.org/pypi/psutil
import psutil
psutil.get_pid_list()
psutil.virtual_memory()
psutil.cpu_times()
等
相關問題
- 1. 獲取系統狀態(ro或rw)
- 2. 如何獲取Qt中的系統網絡狀態?
- 3. 從系統表中獲取節點狀態
- 4. 獲取系統日期和狀態的行
- 5. 系統API來獲取進程使用的內存狀態
- 6. 操作系統狀態圖
- 7. 系統狀態函數C#
- 8. 在R中捕獲退出狀態和系統調用輸出
- 9. 如何在iPhone中獲取3G系統設置或數據漫遊狀態
- 10. 獲取系統
- 11. 在Python中使用Python獲取系統音量(聲級)
- 12. WP7.1中的系統狀態C#
- 13. iOS系統狀態欄中的消息
- 14. Java生態系統的狀態
- 15. 如何動態獲取系統架構?
- 16. 如何動態獲取系統名稱?
- 17. Python:獲取系統日曆格式
- 18. 獲取文件系統上市的Python
- 19. 獲取安裝在系統
- 20. 獲取android系統
- 21. 通過Python獲取Jabber狀態
- 22. Python Pexpect pxssh獲取退出狀態
- 23. python-twitter從狀態獲取圖像
- 24. Python,從運行命令獲取狀態
- 25. 獲取skype在android中的聯繫人列表和狀態
- 26. 如何在Python 2.x中獲取系統默認編碼?
- 27. 在Python中獲取Windows /系統文件夾位置
- 28. 在VCS中獲取系統時間
- 29. 在Vxworks中獲取系統時間
- 30. 在iPhone中獲取系統時間
重複這些問題:http://stackoverflow.com/questions/276052/how-to-get-current-cpu-and-ram-usage- in-python http://stackoverflow.com/questions/466684/how-can-i-return-system-information-in-python/467291 – 2009-08-18 22:41:09