python实现windows内存看门狗程序(带GUI界面)
效果图

1、程序核心
看门狗程序核心:
1、运行特定程序任务进程
2、监控任务管理器上的内存使用率
3、如果超过阈值则关闭该特定程序进程
4、重新开启该特定程序
5、重复过程2持续监控
2、程序流程
1、先编写UI界面,并转成py文件
2、实现具体的功能:获取当前内存,设置内存阈值,设置运行时间、获取目标程序路径、名称、开启监控、监控日志
3、使用nuitka打包成exe文件
3、核心代码
"""
@contact: 微信 1257309054
@file: watchDogMain.py
@time: 2024/1/6 20:40
@author: LDC
"""def run(self):'''监控程序'''while 1:self.qmut.lock()if not self.window.is_run:breakself.qmut.unlock()time.sleep(6) self.window.get_memory() msg = '正在监控【{}】,当前内存:{},内存阈值:{}'.format(self.window.file_name_edit_value,self.window.current_memory_edit_value,self.window.memory_threshold_edit_value)self._signal_monitor.emit(json.dumps({'log': msg})) try:if self.window.current_memory_edit_value > self.window.memory_threshold_edit_value:msg = '内存{}%超过{}%,关闭程序'.format(self.window.current_memory_edit_value,self.window.memory_threshold_edit_value)self._signal_monitor.emit(json.dumps({'log': msg})) while self.is_process_running(self.window.file_name_edit_value):os.popen('taskkill /f /im {}'.format(self.window.file_name_edit_value)) time.sleep(2)time.sleep(5)while 1:if self.is_process_running(self.window.file_name_edit_value):breakmsg = '开始重启【{}】'.format(self.window.file_name_edit_value)self._signal_monitor.emit(json.dumps({'log': msg})) p_array_main = Process(target=run,name='monitor_run',args=(self.window.file_path_edit_value, self.window.file_name_edit_value))p_array_main.daemon = False p_array_main.start()time.sleep(5)if self.is_process_running(self.window.file_name_edit_value):msg = '重启【{}】成功'.format(self.window.file_name_edit_value)self._signal_monitor.emit(json.dumps({'log': msg})) except Exception as e:self._signal_monitor.emit(json.dumps({'log': '出错了,{}'.format(e)}))
4、打包
python -m nuitka --standalone --windows-disable-console --onefile --mingw64 --show-memory --show-progress --nofollow-import-to=pillow,PIL,,pyqt5 --enable-plugin=pyqt5 --output-dir=nuitka_out watchDogMain.py