zoukankan      html  css  js  c++  java
  • python监听资源

    import os, sys, time, csv, datetime
    import psutil
    from pynvml import *
    import pynvml
    
    csvdir = 'F:'
    def run():
        nowtime_csv = time.strftime("%Y%m%d%H%M%S", time.localtime(time.time()))
        csvpath = csvdir + 'pc_resource_' + nowtime_csv + '.csv'
        csv_head = 0
        print('收集电脑使用资源,勿关!')
        while 1:
            memory = get_memory()
            cpu = get_cpu()
            gpu = get_gpu()
            time.sleep(0.5)
        
            # 写入csv
            with open(csvpath, 'a', newline='') as csvfile:
                writer = csv.writer(csvfile)
                nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                if csv_head == 0:
                    # csv文件写入表头
                    writer.writerow(["cpu使用率", "总内存", "已使用内存", "空闲内存","总显存", "已使用显存", "空闲显存","当前时间"])
                    csv_head = 1
                writer.writerow([cpu, memory[0],memory[1],memory[2],gpu[0],gpu[1],gpu[2],nowtime])
                csvfile.close()
        
    
    def get_timer():
        nowtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
        timearray = time.strptime(nowtime, "%Y-%m-%d %H:%M:%S")
        timestamp = int(time.mktime(timearray))
        return timestamp
    
    def get_parsec():
        starttime = datetime.datetime.now()
        time.sleep(1)
        endtime = datetime.datetime.now()
        parsec = (endtime - starttime).seconds
    
    def get_memory():
        mem = psutil.virtual_memory()
        # 系统总内存
        zj = float(mem.total) / 1024 / 1024 / 1024
        
        # 系统已经使用内存
        ysy = float(mem.used) / 1024 / 1024 / 1024
    
        # 系统空闲内存
        kx = float(mem.free) / 1024 / 1024 / 1024
    
        # print('系统总内存:%dGB' % zj)
        # print('系统已经使用内存:%dGB' % ysy)
        # print('系统空闲内存:%dGB' % kx)
        memory = (round(zj,2),round(ysy,2),round(kx,2))
        return memory
    
    def get_cpu():
        cpu = psutil.cpu_percent(interval=0,percpu=False)
        # print('cpu使用率:',cpu)
        return cpu
    
    def get_gpu():
        pynvml.nvmlInit()  #初始化
        handle = pynvml.nvmlDeviceGetHandleByIndex(0)  #0是GPU id
        meminf = pynvml.nvmlDeviceGetMemoryInfo(handle)
        print("Driver Version: ",nvmlSystemGetDriverVersion())  #显示驱动信息
        total = meminf.total/1024/1024
        used = meminf.used/1024/1024
        free = meminf.free/1024/1024
        print("总显存:",total)  #显卡总的显存大小
        print("已使用显存:",used)  #这里是字节bytes,所以要想得到以兆M为单位就需要除以1024**2
        print("未使用显存:",free)  #显卡剩余显存大小
        print("显存数量:",pynvml.nvmlDeviceGetCount())  #显示有几块GPU
        gpu = (round(total,2),round(used,2),round(free,2))
        return gpu
    
    class Logger(object):
        def __init__(self, filename="files/algorithm_test.log"):
            self.terminal = sys.stdout
            self.log = open(filename, "a")
    
        def write(self, message):
            self.terminal.write(message)
            self.log.write(message)
    
        def flush(self):
            pass
    sys.stdout = Logger('files/algorithm_test.log')
    
    if __name__ == "__main__":
        Logger()
        run()
        #memory = get_memory()
        # get_cpu()
        # get_gpu()
        
  • 相关阅读:
    tars framework 源码解读(五) framework 部分章节。PropertyServer,StatServer上报统计服务
    tars framework 源码解读(五) framework 部分章节。NotifyServer 通知服务
    mysql之 percona-xtrabackup 2.4.7安装(热备工具)
    年轻
    C语言整型数据(整数)
    【转】想象5年后的你
    MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
    CSS3——前端预处理技术(Less、Sass、CoffeeScript、TypeScript)
    HTML(三)——本地存储
    HTML5(二)——特殊符号、新增属性、表单重写属性、
  • 原文地址:https://www.cnblogs.com/hjy123/p/14282048.html
Copyright © 2011-2022 走看看