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()
  • 相关阅读:
    选择省市区的组件
    element ui 合计/table show-summary
    双击放大预览功能/组件
    vue 中获取初始的值
    vue 兄弟组件之间通信
    js数组常用到的方法,及其注意事项
    ps
    最有效的学习方法
    css2
    prettytable:像数据库一样格式化输出内容
  • 原文地址:https://www.cnblogs.com/darlingmz/p/12838669.html
Copyright © 2011-2022 走看看