Blogger Information
Blog 85
fans 0
comment 0
visits 72720
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Python 操作系统级别模块Psutil
Smile
Original
439 people have browsed it

  0、安装与导入psutil

  pip install psutil

  import psutil

  1、查看CPU信息

  >>> psutil.cpu_count() #查看CPU核数

  2

  >>> psutil.cpu_count(logical=False) #查看CPU物理个数

  2

  >>> psutil.cpu_percent() #查看CPU使用率

  23.4

  >>> psutil.cpu_percent(percpu=True) #查看每个CPU的使用率

  [18.9, 26.0]

  >>> psutil.cpu_times() #查看CPU时间分配情况

  scputimes(user=2750.625, system=2876.796875, idle=19316.078125)

  2、查看开机时间

  >>> import datetime

  >>>

  datetime.datetime.fromtimestamp(psutil.boot_time()) #查看开机时间

  datetime.datetime(2021, 7, 28, 7, 17, 50, 125000)

  >>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S')

  '2021-07-28 07:17:49'

  3、查看内存信息

  >>> virtual_memory=psutil.virtual_memory()

  >>> virtual_memory.total /1024/1024/1024

  2L

  >>> virtual_memory.used

  1909178368L

  >>> virtual_memory.used/1024/1024/1024

  1L

  >>> virtual_memory.free/1024/1024/1024

  1L

  >>> virtual_memory.percent

  60.0

  >>> psutil.swap_memory()

  sswap(total=4093583360L, used=2750255104L, free=1343328256L, percent=67.2, sin=0, sout=0)

  >>> map(lambda x :x/1024/1024/1024,_)

  [3L, 2L, 1L, 6.258487701416016e-08, 0, 0]

  4、查看磁盘信息

  >>> psutil.disk_partitions() #查看所有分区信息

  [sdiskpart(device='C:\\', mountpoint='C:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='D:\\', mountpoint='D:\\', fstype='CDFS',

  opts='ro,cdrom'), sdiskpart(device='E:\\', mountpoint='E:\\', fstype='', opts='cdrom'), sdiskpart(device='F:\\', mountpoint='F:\\',

  fstype='NTFS', opts='rw,fixed'), sdiskpart(device='G:\\', mountpoint='G:\\', fstype='', opts='cdrom'), sdiskpart(device='H:\\',

  mountpoint='H:\\', fstype='NTFS', opts='rw,fixed'), sdiskpart(device='I:\\', mountpoint='I:\\', fstype='NTFS', opts='rw,fixed')]

  >>> psutil.disk_usage('c:\\') #查看指定分区的磁盘空间情况

  sdiskusage(total=39925473280L, used=33699393536L, free=6226079744L, percent=84.4)

  >>> psutil.disk_io_counters(perdisk=True) #查看硬盘读写操作情况

  {'PhysicalDrive0': sdiskio(read_count=111439, write_count=257966, read_bytes=3963010048L, write_bytes=3945559040L, read_time=101026455L,

  write_time=48928114L)}

  5、查看网络流量与收发包信息

  >>> psutil.net_io_counters()

  snetio(bytes_sent=29524224, bytes_recv=81521587, packets_sent=117718, packets_recv=162906, errin=0, errout=0, dropin=0, dropout=0)

  6、查看当前登录用户信息

  >>> psutil.users()

  [suser(name=u'Administrator', terminal=None, host='0.242.33.0', started=1438039269.0)]

  7、查看进程信息

  >>> psutil.pids() 查看当前所有进程id

  [0, 4, 612, 664, 696, 740, 752, 1008, 1028, 1108, 1232, 1356, 1392, 1428, 1544, 1692, 1756, 1776, 1872, 1928, 412, 668, 2140, 2152, 2164,

  2172, 2192, 2200, 2208, 2224, 2304, 2332, 2352, 2376, 2400, 2448, 2516, 2556, 2588, 2652, 2696, 2756, 2808, 2864, 3080, 3136, 3184, 3192,

  3244, 3264, 3332, 3720, 3796, 3824, 3872, 3908, 3976, 4024, 2688, 2684, 964, 4152, 4568, 4660, 4980, 5412, 5644, 5804, 4396, 4880, 5072,

  6032, 4576, 7036, 5020, 5192, 7548, 1352, 1264, 5816, 8596, 5508, 9124, 8420, 9512, 9416, 2904, 5444, 10696, 11308, 10608, 11564, 10912,

  4204, 12056, 8640, 9248, 11180, 10348]

  >>> p=psutilcess(4204) #获取指定id的进程

  >>> p.name() #进程名

  u'QQBrowser.exe'

  >>> p.username() #查看创建该进程的用户名

  u'HP6531\\Administrator'

  >>> p.cmdline() #查看该进程对应的QQ账号转让平台exe文件

  ['C:\\Program Files\\Tencent\\QQBrowser\\QQBrowser.exe', '-host=tab', '-scope=8420', '-Cred=624', '-group=0', '-tid=10', '-core=1',

  '/prefetch:2']

  >>> p.cwd()

  'C:\\Program Files\\Tencent\\QQBrowser'

  >>> p.exe()

  u'C:\\Program Files\\Tencent\\QQBrowser\\QQBrowser.exe'

  >>> p.cpu_affinity() #该进程CPU占用情况(运行在哪个CPU上)

  [0, 1]

  >>> p.num_threads() #该进程包含的线程数量

  14

  >>> p.threads() #该进程所有线程对象

  [pthread(id=11400, user_time=0.1875, system_time=0.1875), pthread(id=12164, user_time=0.0, system_time=0.0), pthread(id=10636,

  user_time=0.0, system_time=0.0), pthread(id=8964, user_time=0.0, system_time=0.015625), pthread(id=10872, user_time=0.015625,

  system_time=0.046875), pthread(id=12228, user_time=0.0, system_time=0.0), pthread(id=11528, user_time=0.0, system_time=0.0), pthread

  (id=5900, user_time=0.0, system_time=0.0), pthread(id=10472, user_time=0.0, system_time=0.0), pthread(id=11100, user_time=0.0,

  system_time=0.0), pthread(id=4116, user_time=0.0, system_time=0.015625), pthread(id=8544, user_time=0.0, system_time=0.015625), pthread

  (id=8768, user_time=0.0, system_time=0.0), pthread(id=10616, user_time=0.640625, system_time=0.328125)]

  >>> p.status() #进程状态

  'running'

  >>> p.is_running()

  True

  >>> p.suspend() #挂起

  >>> p.status()

  'stopped'

  >>> p.is_running()

  True

  >>> p.resume() #恢复运行

  >>> p.status()

  'running'

  >>> p.kill() #结束进程

  8、作为一个小例子,下面的代码检查记事本程序是否在运行,如果是,则返回记事本程序对应的进程id

  >>> for id in psutil.pids():

  try:

  p=psutilcess(id)

  if os.path.basename(p.exe())=='notepad.exe':

  print id

  except:

  pass

  系统相关功能

  获取CPU数量:

  import psutil # 后面不再加这句代码

  psutil.cpu_count() # cpu数量

  psutil.cpu_count(logical=True) # 逻辑CPU的数量

  获取每个CPU当前利用率:

  psutil.cpu_percent(interval=1) # 1 秒后线束总的利用率结果

  psutil.cpu_percent(interval=0, percpu=True) # 立马显示每个CPU结果

  获取CPU的频率,包括最低、高频率,以及当前频率:

  psutil.cpu_freq(percpu=True) #获取每个CPU的频率

  内存

  获取物理内存的利用情况:

  psutil.virtual_memory() # 有点类似 free 命令

  获取 Swap 交换内存:

  psutil.swap_memory() # 有点类似 free 命令

  硬盘

  获取硬盘分区,返回的是分区格式类型,挂载点:

  psutil.disk_partitions(all=False) #类似lsblk命令

  获取硬盘利用率,返回的是使用多少、还剩多少,以及使用率:

  psutil.disk_usage("C:\") # 查看C盘

  网络

  获取当前网络的IO情况,返回IO的字节、包的数量:

  psutil.net_io_counters()

  获取当前连接数,对于这个需要root用户权限,因此在运行python的时候加上sudo:

  psutil.net_connections() # 返回连接详细信息

  获取网口信息和状态:

  psutil.net_if_addrs() # 获取网络接口信息

  psutil.net_if_stats() # 获取网络接口状态

  进程

  获取所有进程PID:

  psutil.pids() # 所有进程ID

  可以通过指定进程PID来获取,进程名称、进程路径、状态以及结束进程等。

  pid=psutilcess(8888) # 获取进程pid为8888

  的进程

  pid.name() # 进程名称

  pid.status() # 进程状态

  pid.terminate() # 终止经常

  11180

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post