栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python的psutil模块实现PC端内存监控

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python的psutil模块实现PC端内存监控

import psutil
import sys
import os


def get_pid_list():
    pid_list = []
    for i in psutil.pids():  # 用PID遍历本机应用
        try:  # 跳过无name进程的报错
            pro = psutil.Process(i)
            if exe_name in pro.name():
                pid_list.append(pro)
        except:
            pass
    return pid_list

def get_men():
    list1 = []
    for i in get_pid_list():
        list1.append(i.memory_full_info().uss/1024/1024)
    return list1

def get_cpu():
    list1 = []
    for i in get_pid_list():
        list1.append(i.cpu_percent())
    return list1

def get_threads():
    list1 = []
    for i in get_pid_list():
        list1.append(i.num_threads())
    return list1

def get_rows():
    a = len(get_pid_list())
    men = ''
    cpu = ''
    threads = ''
    for i in range(1,a+1):
        men += f' men{str(i)}'
        cpu += f' cpu{str(i)}'
        threads += f' threads{str(i)}'
    return 'time' + men + ' men_total' + cpu + 'cpu_tatol '+ threads + ' threads_total'

def get_timestamp():
    import time
    return str(time.time())

def list_to_str(l):
    ll = ''
    for i in l:
        ll += (' ' + str(i))
    return ll

def write_to_txt(file):
    # os.chdir(r'D:adb')
    if not os.path.exists(file):  # 文件不存在就新建,并写入标签
        with open(file,mode='w') as op:
            op.write(get_rows())
    with open(file, 'a+') as op:  # 逐行写入性能数据
        # time.sleep(1)
        op.write('n' + get_timestamp() + list_to_str(get_men()) + ' ' + str(sum(get_men())) + list_to_str(get_cpu()) + ' ' + str(sum(get_cpu())) + list_to_str(get_threads()) + ' ' +str(sum(get_threads())))

if __name__ == '__main__':
    while True:
        exe_name = sys.argv[2]  # sys.argv 接受外部参数
        write_to_txt(sys.argv[1])



 

import datetime
import subprocess
import sys
import time
from matplotlib import pyplot as plt
import numpy


def get_timestamp():
    return time.strftime("%Y%m%d%H%M%S", time.localtime())


def test_name(file_name):
    exe_name = 'chrome.exe'
    p = subprocess.Popen(
        r'python C:Users86181PycharmProjectszidongget_data.py ' + file_name + ' ' + exe_name)  # cmd命令执行
    return p


def plot_1(file_name):
    data1 = numpy.loadtxt(file_name, skiprows=1, unpack=1)  
# 逐行读取TXT为多维列表,跳过第一行,行列倒置
    with open(file_name, 'r') as op:  # 读取第一行
        label = op.readline().strip().split(' ')
    for i in range(1, len(label)):
        plt.plot(data1[0], data1[i])
        [a, b] = numpy.polyfit(data1[0], data1[i], 1)  # 数据拟合
        new_y = []
        for k in data1[0]:
            new_y.append(a * k + b)
        plt.plot(data1[0], new_y)
        plt.xlabel('time')
        plt.ylabel(label[i])
        plt.title(label[i])
        plt.grid(True)
        plt.savefig(label[i] + '.png')


if __name__ == "__main__":
    file_name = get_timestamp() + '.txt'  
# 时间戳 + 方法名    sys._getframe().f_code.co_name
    p = test_name(file_name)
    # ?????
    time.sleep(10)
    p.kill()
    plot_1(file_name)

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号