V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shuyouning32
V2EX  ›  Python

Python 写了个 GUI 程序,运行久了老是闪退 咋解决

  •  
  •   shuyouning32 · 1 天前 · 2146 次点击
    需求是这样的。

    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}")
    有木有大佬能帮忙看看的!
    15 条回复    2025-03-18 19:46:21 +08:00
    liuhai233
        1
    liuhai233  
       1 天前   ❤️ 1
    定时任务加个 try catch 看看
    lyxxxh2
        2
    lyxxxh2  
       1 天前   ❤️ 1
    gui 不晓得。
    我命令行闪退不怕,因为有错误信息,能否用命令来启动 gui?
    fds
        3
    fds  
       1 天前   ❤️ 1
    没有错误日志?
    qiuhang
        4
    qiuhang  
       1 天前   ❤️ 1
    这没法看,可能的错误原因很多。你别这样写,你把 safe_start 下面的 print(f"任务执行失败: {e}") 换成 import traceback; print(traceback.format_exc()) 看看具体调用栈相关报错信息。最好 timer_task 下面也包一层 try except 捕获多线程调用过程中可能存在的错误信息
    qiuhang
        5
    qiuhang  
       1 天前   ❤️ 1
    @qiuhang 对了,你走命令行调用启动 gui 程序,错误信息就能打印到控制台。或者你调用 logging 写入到日志文件也行,甚至直接 open 个文件写入到文件都可以,总之你得看报错信息才能解决问题。
    araraloren
        6
    araraloren  
       1 天前   ❤️ 3
    你的代码加密了,我看不懂
    GaGim
        7
    GaGim  
       1 天前   ❤️ 1
    你要加个 try catch 捕捉一下异常,看是哪里线程冲突了,也不知道你用哪个 gui 库,贴个多线程代码根本看不出什么。
    fenglirookie
        8
    fenglirookie  
       1 天前   ❤️ 1
    gpt 解决
    Abbeyok
        9
    Abbeyok  
       1 天前   ❤️ 1
    如果是 pyqt 、pyside ,更新 gui 用 signal ,不然很容易退
    calvinHxx
        10
    calvinHxx  
       1 天前   ❤️ 2
    gui 程序闪退 无外乎几个场景:
    1.工作线程操作视图, 视图只能在主线程(gui 线程) 操作
    2.内存泄漏,地址访问错误(python 应该暂时不用管)
    3.栈溢出,爆栈
    4.内存爆了。

    一个一个排查看看。大部分 gui 程序前三点问题居多。
    AccelerXu
        11
    AccelerXu  
       1 天前   ❤️ 1
    多半是内存暴了
    strickland3
        12
    strickland3  
       1 天前   ❤️ 1
    把 max_workers 改小一点试试
    dajj
        13
    dajj  
       1 天前   ❤️ 1
    import faulthandler
    faulthandler.enable() # enable faulththandler catchSIGSEGV , print system error


    在入口文件加上这两行,要求输出致命错误
    shuyouning32
        14
    shuyouning32  
    OP
       1 天前
    程序打包了 exe 没有搞 日志,新手刚入门
    iorilu
        15
    iorilu  
       21 小时 52 分钟前
    肯定是多线程导致得

    用 pyqt 或 pyside 这些东西, 如果要用多线程一定要用 pyqt 框架自带得 qthread 等东西
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4983 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 09:38 · PVG 17:38 · LAX 02:38 · JFK 05:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.