Rumah > Artikel > pembangunan bahagian belakang > Bagaimana untuk menggunakan modul psutil Python
CentOS安装psutil包: python版本:3.5 wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.1.tar.gz --no-check-certificate tar zxvf psutil-3.2.1.tar.gz cd psutil-3.2.1 python setup.py install Windos安装psutil包: D:\python35\Scripts>pip3.exe install psutil Collecting psutil Downloading psutil-5.3.1-cp35-cp35m-win_amd64.whl (215kB) 100% |████████████████████████████████| 225kB 84kB/s Installing collected packages: psutil Successfully installed psutil-5.3.1Mendapat satu data, seperti masa CPU pengguna atau masa menunggu IO, seperti yang ditunjukkan di bawah:
>>> psutil.cpu_times() scputimes(user=650613.02, nice=22.14, system=154916.5, idle=16702285.26, iowait=68894.55, irq=3.38, softirq=7075.65, steal=0.0, guest=0.0) >>>
>>> psutil.cpu_times().user 650617.11 >>> psutil.cpu_times().iowait 68894.63 >>>
#CPU逻辑个数 >>> psutil.cpu_count() 2 #CPU物理个数 >>> psutil.cpu_count(logical=False) 1 >>>
2. Maklumat memori
Kaedah memori_maya digunakan terutamanya untuk mendapatkan maklumat memori. Untuk menggunakan swap, gunakan kaedah swap_memory.>>> psutil.cpu_percent() 2.5 >>> psutil.cpu_percent(1) 2.5 >>>Di mana peratus mewakili bahagian memori sebenar yang digunakan, iaitu (1047543808-717537280)/1047543808*100%. tersedia menunjukkan memori yang masih boleh digunakan.
>>> mem = psutil.virtual_memory() >>> mem svmem(total=4018601984, available=1066205184, percent=73.5, used=3904004096, free=114597888, active=3302174720, inactive=426078208, buffers=156520448, cached=795086848) >>> mem.total 4018601984 >>> mem.used 3904004096 >>> mem.free 114597888 >>> print(mem.total/1024/1024) 3832.4375 >>>Kaedah disk_io_counters lalai mendapat jumlah nombor IO dan membaca dan menulis maklumat cakera keras jika anda perlu mendapatkan data satu partition io dan membaca dan menulis maklumat serta parameter "perdisk=True".
>>> psutil.disk_partitions() [sdiskpart(device='/dev/mapper/root', mountpoint='/', fstype='ext4', opts='rw,errors=remount-ro'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext2', opts='rw')] >>> psutil.disk_usage('/') sdiskusage(total=42273669120, used=17241096192, free=22885195776, percent=40.8) >>>
4. Maklumat rangkaian:
Kaedah menggunakan io rangkaian dan io cakera adalah sama Kaedah net_io_counters digunakan terutamanya maklumat satu kad rangkaian, tambah pernic =Parameter benar.>>> psutil.disk_io_counters() sdiskio(read_count=638190, write_count=77080153, read_bytes=16037795840, write_bytes=1628871606272, read_time=2307367, write_time=1777841305) >>> psutil.disk_io_counters(perdisk=True) {'vdb1': sdiskio(read_count=312, write_count=0, read_bytes=1238016, write_bytes=0, read_time=95, write_time=0), 'vda1': sdiskio(read_count=637878, write_count=77080257, read_bytes=16036557824, write_bytes=1628873314304, read_time=2307272, write_time=1777841879)} >>>
5 Maklumat sistem lain:
1 Dapatkan masa but#获取网络总的io情况 >>> >>> psutil.net_io_counters() snetio(bytes_sent=525490132009, bytes_recv=409145642892, packets_sent=948527563, packets_recv=778182181, errin=0, errout=0, dropin=0, dropout=0) #获取网卡的io情况 >>> >>> psutil.net_io_counters(pernic=True) {'lo': snetio(bytes_sent=56524704027, bytes_recv=56524704027, packets_sent=33602236, packets_recv=33602236, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=468966480940, bytes_recv=352622081327, packets_sent=914930488, packets_recv=744583332, errin=0, errout=0, dropin=0, dropout=0)} >>>2 > 3. Lihat satu proses
##以linux时间格式返回,可以使用时间戳转换 >>> psutil.boot_time() 1496647567.0 #转换成自然时间格式 >>> psutil.boot_time() 1496647567.0 >>> datetime.datetime.fromtimestamp(psutil.boot_time ()).strftime("%Y-%m-%d %H: %M: %S") '2017-06-05 15: 26: 07' >>>Lihat skrip perkakasan sistem:
>>> psutil.pids() [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 46, 47, 48, 49, 50, 51, 52, 53, 60, 61, 63, 64, 65, 97, 98, 279, 280, 331, 398, 481, 676, 693, 769, 845, 848, 1023, 1085, 1108, 1355, 1366, 1457, 1474, 1475, 1494, 1541, 1543, 1545, 1546, 1548, 1550, 1552, 2829, 12436, 12913, 13129, 16022, 16029, 16030, 16031, 16032, 16033, 16518, 16520, 17088, 17124, 19203, 25382, 32679]
Atas ialah kandungan terperinci Bagaimana untuk menggunakan modul psutil Python. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!