zoukankan      html  css  js  c++  java
  • 【python】Psutil指定Pid监控机器cpu和内存

    借鉴地址:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984

    import smtplib
    from email import encoders
    from email.mime.base import MIMEBase
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    
    import psutil
    from datetime import datetime
    import time
    from matplotlib import pyplot as plt
    import csv
    
    from util.http_rtc_until import get_time, send
    
    pid = 9214
    proc = psutil.Process(pid)
    
    def timer():
        out = open("timer.csv", 'w')
        start_time = int(time.time())
        while True:
            x = datetime.now().strftime("%M:%S")
            time.sleep(1)
            y = proc.memory_info().rss / 1024 / 1024
            print(x,'Used Memory:', y, 'MB')
    
            list = [x, y]
            csv_write = csv.writer(out)
    
            csv_write.writerow(list)
            end_time = int(time.time())
            if end_time - start_time > 5:
                break
    
    def pic():
        with open("timer.csv") as Filename:
            reader = csv.reader(Filename)
            x = []
            y = []
            for row in reader:
                x.append(row[0])
                y.append(row[1])
    
        plt.plot(x, y)
        plt.xticks(x, x, rotation=60)
        plt.rcParams["figure.figsize"] = (8.0,8.0)
        plt.savefig('test.png')
        image = 'test.png'
        plt.show()
    
        # send("meizhuo@conew.com","来自251的图片",image)
        sender = "meizhuo@conew.com"
        receiver = "meizhuo@conew.com"
        password = "cbidcdmrsnupphtf"   #密钥
        smtp = "smtp.gmail.com"
        port = "465"    #gmai固定端口号
    
        message = MIMEMultipart()
        message['From'] = sender
        message['To'] = receiver
        message['Subject'] = Header("251的图片", 'UTF-8')  # 邮件主题
    
        #附件带图片
        with open(image, 'rb') as f:
            mime = MIMEBase('image', 'image', filename=image)
            mime.add_header('Content-Disposition', 'attachment', filename=image)
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
            message.attach(mime)
    
        smtp_obj = smtplib.SMTP_SSL(smtp, port)
        smtp_obj.login(sender, password)
        receiver and smtp_obj.sendmail(sender, receiver, message.as_string())
        print("success")
    
    timer()
    pic()
  • 相关阅读:
    操作系统设计与实现(二)
    SpringCloud(八)Consul的微服务注册
    图的实现(邻接矩阵)及DFS、BFS
    SpringCloud(七)服务注册之Consul的简介和原理
    Mybatis笔记目录(6天)
    Mybatis学习笔记——day02
    C语言教程Day01
    Linux C/C++方向开发(13周学习路线)
    基于Java的实验室预约管理系统
    基于Android的高校学生考勤系统的设计与实现
  • 原文地址:https://www.cnblogs.com/darlingmz/p/12838669.html
Copyright © 2011-2022 走看看