需求是这样的。gui 对象中有个 tableList 数组,数组中有个对象 执行 线程 run 任务。gui 定时了一个任务 每 1 分钟去刷 数组对象 的 run 任务刷久了 一晚上 起来看不到程序了。 头疼死了。# 定时任务def timer_task(self):# 定时任务的具体逻辑print("定时任务执行中...")# 使用线程池并发执行# with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:# futures = [executor.submit(instance.start) for instance in self.tableList]# # 等待所有线程完成# for future in concurrent.futures.as_completed(futures):# future.result()with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor: # 调整线程池大小futures = []for instance in self.tableList:if not self.task_queue.full(): # 检查队列是否已满self.task_queue.put(instance)future = executor.submit(self.safe_start, instance)futures.append(future)# 等待所有线程完成for future in concurrent.futures.as_completed(futures):future.result()self.task_queue.get() # 任务完成后从队列中移除# 执行任务def safe_start(self, instance):with self.lock: # 使用线程锁保护共享资源try:instance.start()except Exception as e:print(f"任务执行失败: {e}")有木有大佬能帮忙看看的!
这没法看,可能的错误原因很多。你别这样写,你把 safe_start 下面的 print(f"任务执行失败: {e}") 换成 import traceback; print(traceback.format_exc()) 看看具体调用栈相关报错信息。最好 timer_task 下面也包一层 try except 捕获多线程调用过程中可能存在的错误信息
gui 程序闪退 无外乎几个场景:1.工作线程操作视图, 视图只能在主线程(gui 线程) 操作2.内存泄漏,地址访问错误(python 应该暂时不用管)3.栈溢出,爆栈4.内存爆了。一个一个排查看看。大部分 gui 程序前三点问题居多。
import faulthandlerfaulthandler.enable() # enable faulththandler catchSIGSEGV , print system error在入口文件加上这两行,要求输出致命错误