Python 写了个 GUI 程序,运行久了老是闪退 咋解决 - V2EX
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 210 天前 3881 次点击
    这是一个创建于 210 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需求是这样的。

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

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


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

    用 pyqt 或 pyside 这些东西, 如果要用多线程一定要用 pyqt 框架自带得 qthread 等东西
    jackOff
        16
    jackOff  
       190 天前
    内存回收机制吧,主要是 python 会无视 pyqt 的底层 c++语法直接强制回收 qt 控件(有时候就是一个 paint 刷新就触发了,具体原因不明),所以语法上要特别注意必须显式声明每个对象的父类,我习惯上搞一个全局变量存放控件实例,这里建议你的线程池不使用 with 语法,改成声明一个对象:self.executor
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5447 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 35ms UTC 09:11 PVG 17:11 LAX 02:11 JFK 05:11
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86