Skip to content

Instantly share code, notes, and snippets.

@Leetroch Leetroch/ram_monitor
Created Jan 13, 2020

Embed
What would you like to do?
Personal_memory_monitor_script
from typing import List
import psutil
import math
import time
def occ_rate() ->float:
memory = psutil.virtual_memory()
occ_rate = float(memory.used) / float(memory.total) * 100
return float(occ_rate)
'_______________________________________________________________________________'
def memory_panel():
while 1:
memory = psutil.virtual_memory()
ratio = math.pow(1024, 3) # X Byte = X GB / 1024^3
print('已使用内存:%.2f GB' % (memory.used / ratio))
'''需要一个利用上行数据进行统计得出最大值和最小值以及超出设定阈值并记录次数。'''
print('总内存:%.2f GB' % (memory.total / ratio))
'occ_rate = float(memory.used) / float(memory.total) * 100' # 需要抽出来单独做一个函数?
'''occ_rate代表内存占用率'''
m_rate = occ_rate()
print("内存占用率: %.2f%%" % m_rate) # .2f是表示保留两位小数。
print(psutil.swap_memory())
print("退出请输入exit(请区分大小写): ")
chosen = input() # input()方法将所有输入值转换为string类型
if chosen == "exit":
print('再见')
exit()
else:
continue
'_______________________________________________________________________________'
def occ_warning_recorder(rate):
"""
:type rate: float
"""
global file
global now2
occ_warning: List[float] = []
while 1:
rate = occ_rate()
if rate < 0.95:
time.sleep(10)
pass
elif rate >= 0.95:
rate: float = rate
print('/a')
# length_of_w = len(occ_warning)
for i in range(0,100):
occ_warning[i] = rate
i += 1
now = int(round(time.time() * 1000))
now2 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now / 1000))
"上面这两行检测通过,数据正常;occ_warning为警告值"
file = open('G:/python练习册/memory_float_log.txt',
'a') # 写入警告值到log.txt当中,在'w'写入模式下,当我们下次写入变量时,会覆盖txt,有一个追加模式'a',可以实现多次写入
output_data = [now2, occ_warning[i], '%']
"—".join(output_data)
file.write(str(output_data))
# 最傻瓜的连接字符串方式,但不推荐用这种方式file.write(str(now02) + '|' + str(occwarning[i]) + '%')
file.write('/n')
file.close()
'_________________________________________________________________________________________________'
print(memory_panel())
occupied: float = occ_rate()
occ_warning_recorder(occupied)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.