技术标签: python psutil模块
psutil
1.简单介绍
2.安装
[[email protected]_46_121_centos python]# pip3 install psutil
我安装的是ipsutil-5.2.2这个版本!
3.基本使用
3.1 cpu相关
In [1]: import psutil
In [2]: psutil.cpu_times()#使用cpu_times获取cpu的完整信息
Out[2]: scputimes(user=769.84, nice=2.78, system=387.68, idle=83791.98, iowait=479.84, irq=0.0, softirq=0.81, steal=0.0, guest=0.0, guest_nice=0.0)
In [3]: psutil.cpu_count()#获取cpu的逻辑个数
Out[3]: 1
In [4]: psutil.cpu_times_percent()#获取cpu的所有逻辑信息
Out[4]: scputimes(user=0.7, nice=0.0, system=0.5, idle=98.4, iowait=0.4, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
3.2内存相关
In [5]: psutil.virtual_memory()#获取内存的所有信息
Out[5]: svmem(total=1041309696, available=697958400, percent=33.0, used=176611328, free=91947008, active=516075520, inactive=323096576, buffers=102719488, cached=670031872, shared=12873728)
In [7]: psutil.virtual_memory().total
Out[7]: 1041309696
In [8]: psutil.virtual_memory().used
Out[8]: 176553984
In [9]: psutil.virtual_memory().free
Out[9]: 91901952
In [13]: psutil.swap_memory()#交换分区相关
Out[13]: sswap(total=0, used=0, free=0, percent=0, sin=0, sout=0)
3.3磁盘相关
In [14]: psutil.disk_partitions()#获取磁盘的详细信息
Out[14]: [sdiskpart(device='/dev/vda1', mountpoint='/', fstype='ext3', opts='rw,noatime,data=ordered')]
In [17]: psutil.disk_usage('/')#获取分区的使用情况
Out[17]: sdiskusage(total=21002579968, used=2223321088, free=17705578496, percent=11.2)
In [18]: psutil.disk_io_counters()#获取磁盘总的io个数,读写信息
Out[18]: sdiskio(read_count=25365, write_count=118754, read_bytes=391898112, write_bytes=3048738816, read_time=343585, write_time=10775463, read_merged_count=107, write_merged_count=583537, busy_time=623556)
补充说明下:
read_count(读IO数)
write_count(写IO数)
read_bytes(读IO字节数)
write_bytes(写IO字节数)
read_time(磁盘读时间)
write_time(磁盘写时间)
3.4网络信息
In [19]: psutil.net_io_counters()#获取网络总信息
Out[19]: snetio(bytes_sent=24172706, bytes_recv=168785879, packets_sent=163657, packets_recv=442827, errin=0, errout=0, dropin=0, dropout=0)
In [20]: psutil.net_io_counters(pernic=True)#获取每个网络接口的信息
Out[20]:
{'eth0': snetio(bytes_sent=24177750, bytes_recv=168797166, packets_sent=163685, packets_recv=442948, errin=0, errout=0, dropin=0, dropout=0),
'lo': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
3.5其它信息
In [21]: psutil.users()#返回当前登录系统的用户信息
Out[21]: [suser(name='root', terminal='pts/0', host='X.X.X.X', started=1492844672.0)]
In [22]: psutil.boot_time()#获取开机时间
Out[22]: 1492762895.0
In [23]: import datetime#转换成你能看懂的时间
In [24]: datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
Out[24]: '2017-04-21 16:21:35'
4.系统进程的管理方法
[root@VM_46_121_centos ~]# ps -ef | grep ipython#这里首先我用ps获取ipython进程号
root 2407 2365 0 16:50 pts/1 00:00:00 grep --color=auto ipython
root 29735 26143 0 16:01 pts/0 00:00:07 /usr/local/bin/python3.5 /usr/local/bin/ipython
In [27]: psutil.Process(29735)
Out[27]:
In [28]: p=psutil.Process(29735)#实例化一个进程对象,参数为ipython这个进程的PID
In [29]: p.name()#获得进程名
Out[29]: 'ipython'
In [31]: p.exe()#获得进程的bin路径
Out[31]: '/usr/local/bin/python3.5'
In [32]: p.cwd()获得进程工作目录的绝对路径
Out[32]: '/usr/local/lib/python3.5/site-packages/psutil-5.2.2-py3.5.egg-info'
In [33]: p.status()#获得进程状态
Out[33]: 'running'
In [34]: p.create_time()#获得进程创建的时间,时间戳格式
Out[34]: 1492848093.13
In [45]: datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")
Out[45]: '2017-04-22 16:01:33'
In [35]: p.uids()#获取进程的uid信息
Out[35]: puids(real=0, effective=0, saved=0)
In [36]: p.gids()#获取进程的gid信息
Out[36]: pgids(real=0, effective=0, saved=0)
In [37]: p.cpu_times()#获取进程的cpu的时间信息
Out[37]: pcputimes(user=9.53, system=0.34, children_user=0.0, children_system=0.0)
In [38]: p.cpu_affinity()#获取进程的cpu亲和度
Out[38]: [0]
In [39]: p.memory_percent()#获取进程的内存利用率
Out[39]: 6.187014703452833
In [40]: p.memory_info()#获取进程的内存rss,vms信息
Out[40]: pmem(rss=64425984, vms=304410624, shared=4755456, text=2465792, lib=0, data=201437184, dirty=0)
In [41]: p.io_counters()#获取进程的io信息
Out[41]: pio(read_count=6915, write_count=6246, read_bytes=73728, write_bytes=1658880, read_chars=9329720, write_chars=1797027)
In [43]: p.num_threads()获取进程开启的线程数
Out[43]: 1
popen类的使用:获取用户启动的应用程序的进程信息
In [55]: from subprocess import PIPE
In [50]: p1=psutil.Popen(["/usr/bin/python","-c","print('hello')"], stdout=PIPE)
In [51]: p1.name()
Out[51]: 'python'
In [52]: p1.username()
Out[52]: 'root'
In [53]: p1.communicate()
Out[53]: (b'hello\n', None)
In [54]: p.cpu_times()
Out[54]: pcputimes(user=13.11, system=0.52, children_user=0.01, children_system=0.0)
一、Animations介绍Animations是一个实现android UI界面动画效果的API,Animations提供了一系列的动画效果,可以进行旋转、缩放、淡入淡出等,这些效果可以应用在绝大多数的控件中。 二、Animations的分类Animations从总体上可以分为两大类:1.Tweened Animations:该类Animations提供了旋转、移
(php版本5.1.6,安装目录/usr/local/php,源代码目录/usr/software/php5.1.6):1、用cd命令进入php的源代码目录下的etc/mbstring目录下,即“/usr/software/php5.1.6/etc/mbstring”;2、执行 #/usr/local/php/bin/phpize 3、执行 #./configure --with-php-
整合Spring Cloud Alibaba前言开始整合版本与兼容性Spring Cloud Alibaba 版本说明Spring Cloud 版本说明整合Spring Cloud整合Spring Cloud Alibaba完整pom文件项目编译项目源码前言Spring Cloud Alibaba 是 SpringCloud的子项目,也是阿里巴巴的微服务解决方案,致力于提供微服务开发的一站式解决方案,包括微服务开发的必备组件。Spring Cloud Alibaba 的整合包括两步:(整合Spring
本篇文章中,我介绍了matlab矩阵和幻方矩阵的基础知识,大家想要了解的或是复习的都可以来看看,说不定就有所收获。
前言在前两个文章中,我们分析了数据库的连接启动与数据库底层 CRUD 的原理,底层数据库服务支持原生 sql 的运行。本文以 mysql 为例,向大家讲述支持 Fluent 的查询构造器 query 与语法编译器 grammer 的原理。DB::table 与 查询构造器若是不想使用原生的 sql 语句,我们可以使用 DB::table 语句,该语句会返回一个 query 对象:public function table($table){ return $this->query()-
一、jsxjsx的babel配置如果我们希望在项目中使用jsx,那么我们需要添加对jsx的支持: jsx我们通常会通过Babel来进行转换(React编写的jsx就是通过babel转换的); 对于Vue来说,我们只需要在Babel中配置对应的插件即可; 安装Babel支持Vue的jsx插件:npm install @vue/babel-plugin-jsx -D在babel.config.js配置文件中配置插件:jsx计数器案...
Vite+Vue3+Electron构建客户端桌面应用
文章目录1. 前言1.1 常见误区1.2 解决方法2. 统计学习的基本概念2.1 统计学习的定义2.2 统计学习的重要特点2.3 统计学习的对象2.4 统计学习的目的3. 统计学习的分类4. 统计学习方法三要素5. 模型评估与模型选择6. 泛化能力7. 生成模型与判别模型1. 前言 本课程主要是对李航老师的《统计学习方法》第二版进行深入的讲解。工欲善其事,必先利其器。所以为了帮助初学者提高学习效率,先简单讲解一下学习的误区和要点。1.1 常见误区 误区一:在学习过程中总想达到面面俱到,也就是必须
2022年9月 最新版本下 windows terminal + oh-my-posh配置美化教程
在Asp.Net2.0中保存数据库链接字符串六月 29th, 2007 Filed under .NET开发, Asp.Net原文:http://my.donews.com/buffalo319/2007/06/29/%E5%9C%A8aspnet20%E4%B8%AD%E4%BF%9D%E5%AD%98%E6%95%B0%E6%8D%AE%E5%BA%93%E9%93%BE%...
1.首先在项目res目录下新建xml目录,并新建file_paths.xml,这个文件主要用来配置应用共享文件的路径name="root"path="" />name="files"path="path" />name="cache"path="path" />name="external_file_path"path="path" />name="external_cac...
该变量为android4.2中的方法,因为surface等相关改动,在android4.4出现该问题。解决:ANDROID_VIEW_SURFACE_JNI_ID 改为”mNativeObject”