zoukankan      html  css  js  c++  java
  • 【原创】用python写的一个监测本地进程CPU占用的程序

    #coding=utf-8
    import psutil
    import sys
    import time
    try:
    #输入需要监测的进程PID
    PID = raw_input('ProcessPID: ')
    def get_cpu_info():
    reload(sys)
    sys.setdefaultencoding('utf-8')
    #将结果记录到本地文本
    text = open('D:\\CPUresult.txt', 'w')
    i = 0
    #博主新手靠这样来现实循环
    while i < 100000000000000:
    i = i + 1
    #找出本机CPU的逻辑核个数
    cpucount = psutil.cpu_count(logical=True)
    #传入进程PID,实现监测功能
    process = psutil.Process(int(PID))
    cpupercent = process.cpu_percent(interval=2)
    #得到进程CPU占用,同资源检测管理器的数据
    cpu = int(cpupercent / cpucount)
    if cpu <= 50:
    print u'CPU使用率:%s%%' % cpu + ' ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print>> text, u'CPU使用率:%s%%' % cpu + ' ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    else:
    print u'CPU使用率:%s%%,占用率过高' % cpu + ' ' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
    print>> text, u'CPU使用率:%s%%,占用率过高' % cpu + ' ' + time.strftime('%Y-%m-%d %H:%M:%S',
    time.localtime())
    text.close()
    print u'进程%s的' % PID + u'cpu监控已经运行,结果将在D:\\result.txt生成'
    time.sleep(1)
    print "-------------------------------------------------"
    print get_cpu_info()
    finally:
    print u'进程%s' % PID + u'已经结束'
    raw_input()
    面朝大海--\(˙<>˙)/--落霞与孤鹜齐飞
  • 相关阅读:
    用JS获取地址栏参数的方法(超级简单)
    返回前一页并刷新页面方法
    js 弹出确认 取消对话框
    微信“无法回答问题设置”失效
    百度地图api学习平台
    JQuery 定时器 (Jquery Timer 插件)
    select 框option添加属性 js计算价格 保持两位小数
    修改织梦分页标签样式
    一键导航
    关于memset赋值无穷大无穷小
  • 原文地址:https://www.cnblogs.com/huangxiaocheng/p/7068681.html
Copyright © 2011-2022 走看看