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()
  • 相关阅读:
    自己遇到的冲突及解决方案
    怎么解决代码冲突及切换分支
    程序员修养
    代码回退
    gitlab两种连接方式:ssh和http配置介绍
    gitlab创建项目及分支
    github,gitlab的区别
    代码托管有什么用
    新手搭建云服务器详细过程
    UNP学习笔记(第十一章 名字与地址转换)
  • 原文地址:https://www.cnblogs.com/darlingmz/p/12838669.html
Copyright © 2011-2022 走看看