有什么办法可以在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