简易祛火软件代码设计

2501_92582130 2025-06-25 11:05:36

import psutil import time import os import subprocess class CoolingSoftware: def __init__(self): self.max_temp = 70 # 安全温度阈值(℃) self.high_temp = 80 # 高温警告阈值(℃) self.critical_temp = 90 # 危险温度阈值(℃) self.check_interval = 5 # 检查间隔(秒) def get_cpu_temperature(self): """获取CPU温度""" try: # Linux系统 if os.path.exists('/sys/class/thermal/thermal_zone0/temp'): with open('/sys/class/thermal/thermal_zone0/temp', 'r') as f: temp = float(f.read()) / 1000 return temp # Windows系统 - 使用WMI elif os.name == 'nt': import wmi w = wmi.WMI(namespace="root\\wmi") temp_list = w.MSAcpi_ThermalZoneTemperature() return temp_list[0].CurrentTemperature / 10 - 273.15 # MacOS系统 else: return float(subprocess.check_output(["sysctl", "-n", "machdep.xcpm.cpu_thermal_level"])) / 100 except: return None def get_cpu_usage(self): """获取CPU使用率""" return psutil.cpu_percent(interval=1) def take_cooling_actions(self, temp): """根据温度采取降温措施""" if temp > self.critical_temp: print(f"⚠️ 危险高温: {temp}℃ - 紧急降温措施!") self.emergency_cooling() elif temp > self.high_temp: print(f"⚠️ 高温警告: {temp}℃ - 采取降温措施") self.high_temp_actions() elif temp > self.max_temp: print(f"⚠️ 温度过高: {temp}℃ - 建议降温") self.moderate_temp_actions() else: print(f"✅ 温度正常: {temp}℃") def moderate_temp_actions(self): """中等温度采取的措施""" # 可以添加更多措施 pass def high_temp_actions(self): """高温采取的措施""" # 降低CPU性能 if os.name == 'nt': # Windows subprocess.run(["powercfg", "/setacvalueindex", "SCHEME_CURRENT", "CPU", "54533251-82be-4824-96c1-47b60b740d00", "0"]) subprocess.run(["powercfg", "/setdcvalueindex", "SCHEME_CURRENT", "CPU", "54533251-82be-4824-96c1-47b60b740d00", "0"]) subprocess.run(["powercfg", "/s", "SCHEME_CURRENT"]) else: # Linux/Mac pass def emergency_cooling(self): """紧急降温措施""" # 终止高CPU使用率的进程 for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']): if proc.info['cpu_percent'] > 50: try: proc.kill() except: pass def run(self): """主运行循环""" print("简易祛火软件启动...") try: while True: temp = self.get_cpu_temperature() if temp is not None: usage = self.get_cpu_usage() print(f"CPU温度: {temp:.1f}℃, 使用率: {usage:.1f}%") self.take_cooling_actions(temp) else: print("无法获取CPU温度") time.sleep(self.check_interval) except KeyboardInterrupt: print("软件已关闭") if __name__ == "__main__": app = CoolingSoftware() app.run()

...全文
3 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3

社区成员

发帖
与我相关
我的任务
社区描述
开源量化系统介绍,量化指标学习,投资经验分享
java 个人社区 浙江省·宁波市
社区管理员
  • 临水逸
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧