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()
    面朝大海--\(˙<>˙)/--落霞与孤鹜齐飞
  • 相关阅读:
    BCP 命令
    模板复习【updating】
    bzoj3716/4251 [PA2014]Muzeum
    bzoj4318 OSU!
    uoj308 【UNR #2】UOJ拯救计划
    bzoj4695 最假女选手
    省队集训 Day7 选点游戏
    hdu5828 Rikka with Sequence
    bzoj2482 [Spoj1557] Can you answer these queries II
    省队集训 Day6 序列
  • 原文地址:https://www.cnblogs.com/huangxiaocheng/p/7068681.html
Copyright © 2011-2022 走看看