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()
  • 相关阅读:
    电脑无损换硬盘,不用重装系统驱动的小技巧
    OSPF协议原理及配置5-LSA分析
    OSPF协议原理及配置3-邻居关系的建立
    OSPF协议原理及配置2-理解邻居和邻接关系
    我在华为写代码
    嵌入式未来发展
    blog to live,do not love to blog
    浮点数转换为新类型时必须做范围检查
    分享点干货
    基础C语言知识串串香14☞增补知识
  • 原文地址:https://www.cnblogs.com/darlingmz/p/12838669.html
Copyright © 2011-2022 走看看