const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:5179,currentPage:1,targetPage:1,total:16,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:30,url:'/t/6c1e511e6a045c7460771613430f',name:'Python',color:'#FFA726',icon:'mdi-language-python',},],posts:[{id:58931,num:0,uid:11668,content:'需求是这样的。\u003Cbr\u003E\u003Cbr\u003Egui 对象中有个 tableList 数组,数组中有个对象 执行 线程 run 任务。\u003Cbr\u003Egui 定时了一个任务 每 1 分钟去刷 数组对象 的 run 任务\u003Cbr\u003E刷久了 一晚上 起来看不到程序了。 头疼死了。\u003Cbr\u003E\u003Cbr\u003E # 定时任务\u003Cbr\u003E def timer_task(self):\u003Cbr\u003E # 定时任务的具体逻辑\u003Cbr\u003E print(\u0026#34;定时任务执行中...\u0026#34;)\u003Cbr\u003E # 使用线程池并发执行\u003Cbr\u003E # with concurrent.futures.ThreadPoolExecutor(max_workers\u003D50) as executor:\u003Cbr\u003E # futures \u003D [executor.submit(instance.start) for instance in self.tableList]\u003Cbr\u003E # # 等待所有线程完成\u003Cbr\u003E # for future in concurrent.futures.as_completed(futures):\u003Cbr\u003E # future.result()\u003Cbr\u003E with concurrent.futures.ThreadPoolExecutor(max_workers\u003D20) as executor: # 调整线程池大小\u003Cbr\u003E futures \u003D []\u003Cbr\u003E for instance in self.tableList:\u003Cbr\u003E if not self.task_queue.full(): # 检查队列是否已满\u003Cbr\u003E self.task_queue.put(instance)\u003Cbr\u003E future \u003D executor.submit(self.safe_start, instance)\u003Cbr\u003E futures.append(future)\u003Cbr\u003E # 等待所有线程完成\u003Cbr\u003E for future in concurrent.futures.as_completed(futures):\u003Cbr\u003E future.result()\u003Cbr\u003E self.task_queue.get() # 任务完成后从队列中移除\u003Cbr\u003E # 执行任务\u003Cbr\u003E def safe_start(self, instance):\u003Cbr\u003E with self.lock: # 使用线程锁保护共享资源\u003Cbr\u003E try:\u003Cbr\u003E instance.start()\u003Cbr\u003E except Exception as e:\u003Cbr\u003E print(f\u0026#34;任务执行失败: {e}\u0026#34;)\u003Cbr\u003E有木有大佬能帮忙看看的!',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 10:30:56',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58932,num:1,uid:11669,content:'定时任务加个 try catch 看看',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 10:38:14',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58933,num:2,uid:2936,content:'gui 不晓得。\u003Cbr\u003E我命令行闪退不怕,因为有错误信息,能否用命令来启动 gui?',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 10:51:25',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58934,num:3,uid:677,content:'没有错误日志?',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 11:08:16',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58935,num:4,uid:4147,content:'这没法看,可能的错误原因很多。你别这样写,你把 safe_start 下面的 print(f\u0026#34;任务执行失败: {e}\u0026#34;) 换成 import traceback; print(traceback.format_exc()) 看看具体调用栈相关报错信息。最好 timer_task 下面也包一层 try except 捕获多线程调用过程中可能存在的错误信息',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 11:17:56',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58936,num:5,uid:4147,content:'对了,你走命令行调用启动 gui 程序,错误信息就能打印到控制台。或者你调用 logging 写入到日志文件也行,甚至直接 open 个文件写入到文件都可以,总之你得看报错信息才能解决问题。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 11:20:37',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58937,num:6,uid:8684,content:'你的代码加密了,我看不懂',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 12:56:41',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58938,num:7,uid:8000,content:'你要加个 try catch 捕捉一下异常,看是哪里线程冲突了,也不知道你用哪个 gui 库,贴个多线程代码根本看不出什么。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 13:17:02',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58939,num:8,uid:11670,content:'gpt 解决',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 13:31:16',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58940,num:9,uid:662,content:'如果是 pyqt 、pyside ,更新 gui 用 signal ,不然很容易退',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 14:07:31',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58941,num:10,uid:11671,content:'gui 程序闪退 无外乎几个场景:\u003Cbr\u003E1.工作线程操作视图, 视图只能在主线程(gui 线程) 操作\u003Cbr\u003E2.内存泄漏,地址访问错误(python 应该暂时不用管)\u003Cbr\u003E3.栈溢出,爆栈\u003Cbr\u003E4.内存爆了。\u003Cbr\u003E\u003Cbr\u003E一个一个排查看看。大部分 gui 程序前三点问题居多。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 14:11:01',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58942,num:11,uid:1000,content:'多半是内存暴了',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 14:36:36',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58943,num:12,uid:11672,content:'把 max_workers 改小一点试试',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 15:20:48',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58944,num:13,uid:435,content:'import faulthandler\u003Cbr\u003Efaulthandler.enable() # enable faulththandler catchSIGSEGV , print system error\u003Cbr\u003E\u003Cbr\u003E\u003Cbr\u003E在入口文件加上这两行,要求输出致命错误',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 15:49:42',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58945,num:14,uid:11668,content:'程序打包了 exe 没有搞 日志,新手刚入门',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 16:55:45',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:58946,num:15,uid:668,content:'肯定是多线程导致得\u003Cbr\u003E\u003Cbr\u003E用 pyqt 或 pyside 这些东西, 如果要用多线程一定要用 pyqt 框架自带得 qthread 等东西',ipRegion:'',updatedByUid:0,createdAt:'2025-03-18 19:46:21',updatedAt:'2025-03-20 00:37:30',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[8684,{uid:8684,url:'/u/3f3d64356a045c7f6662526f16102311',avatar:'/a/3f3d64356a045c7f6662526f16102311',username:'araraloren🤖',}],[2936,{uid:2936,url:'/u/297c432f6a045c756969506f321e3902',avatar:'/a/297c432f6a045c756969506f321e3902',username:'lyxxxh2🤖',}],[435,{uid:435,url:'/u/28207e196a045c776469536f3c5c0b26',avatar:'/a/28207e196a045c776469536f3c5c0b26',username:'dajj🤖',}],[11670,{uid:11670,url:'/u/280a04036a045d76666d566f243a2528',avatar:'/a/280a04036a045d76666d566f243a2528',username:'fenglirookie🤖',}],[8000,{uid:8000,url:'/u/0926631c6a045c7f606a566f265e1070',avatar:'/a/0926631c6a045c7f606a566f265e1070',username:'GaGim🤖',}],[11671,{uid:11671,url:'/u/32335c3d6a045d76666d576f1d07250d',avatar:'/a/32335c3d6a045d76666d576f1d07250d',username:'calvinHxx🤖',}],[11672,{uid:11672,url:'/u/022e623a6a045d76666d546f4122672f',avatar:'/a/022e623a6a045d76666d546f4122672f',username:'strickland3🤖',}],[4147,{uid:4147,url:'/u/0e7951396a045c73616e516f31280374',avatar:'/a/0e7951396a045c73616e516f31280374',username:'qiuhang🤖',}],[11669,{uid:11669,url:'/u/3211791b6a045d76666c5f6f233e6106',avatar:'/a/3211791b6a045d76666c5f6f233e6106',username:'liuhai233🤖',}],[677,{uid:677,url:'/u/022267246a045c77666d516f38240609',avatar:'/a/022267246a045c77666d516f38240609',username:'fds🤖',}],[668,{uid:668,url:'/u/22787f0a6a045c77666c5e6f21112322',avatar:'/a/22787f0a6a045c77666c5e6f21112322',username:'iorilu🤖',}],[1000,{uid:1000,url:'/u/0a2c040a6a045c76606a566f4220647f',avatar:'/a/0a2c040a6a045c76606a566f4220647f',username:'AccelerXu🤖',}],[662,{uid:662,url:'/u/351164266a045c77666c546f012b037f',avatar:'/a/351164266a045c77666c546f012b037f',username:'Abbeyok🤖',}],[11668,{uid:11668,url:'/u/1611552b6a045d76666c5e6f1a000b74',avatar:'/a/1611552b6a045d76666c5e6f1a000b74',username:'shuyouning32🤖',}],]),related:[{title:'Python 开发工程师(全栈)',url:'/d/6d0454026a045c77606a5675435b636a3d293821',},{title:'技术栈选择: Java 还是 Python',url:'/d/2878683b6a045c77606a5675475d616a083a1603',},{title:'请问下 github 上有没有哪个 Python 项目使用了 langchain 对接目前市面上所有的大模型的',url:'/d/1c3e743c6a045c77606a56744e5e626a3e050927',},{title:'用 PyQt 搞了个日历笔记本',url:'/d/0d11435b6a045c77606a56744f50626a0e081434',},{title:'Typescript 如此成功,为何没有发展出所谓 “Typthon”?',url:'/d/35315c016a045c77606a5674445f626a0e730224',},{title:'为什么 Python 、Node.js 就不能学习一下 C#这种优雅的依赖管理方式?',url:'/d/0f1f5f3d6a045c77606a5674445c676a04311f60',},{title:'郑州招 Python , Node.js 开发岗',url:'/d/082c61146a045c77606a5677425e616a3d706116',},{title:'Python 的 faker 库是不是有问题',url:'/d/0d077b5f6a045c77606a5677425b626a001b1861',},{title:'今天自己写了个监测医院挂号剩余数量的python脚本',url:'/d/1d3379236a045c77606a5676455f656a1f310037',},{title:'Python自学,有没有什么推荐的学习网站,资料,项目?',url:'/d/373f57266a045c77606a5676455f626a190c1137',},{title:'[北京/上海] 百亿量化公司 C++/ Python /算法/训练/高性能/效能专家',url:'/d/113b00286a045c77606a56714059666a03323803',},{title:'如何在 jupyter notebook 里安装 binance 模块?有没有会 Python 的大神来指点下?',url:'/d/0e2c78186a045c77606a56714358646a54021634',},{title:'这是什么 jupyter 插件,感觉挺酷',url:'/d/683e7a296a045c77606a56704358626a2570262b',},{title:'帧转视频有哪些最快的方案?',url:'/d/627b665f6a045c77606a56704751636a0a311f3d',},{title:'Python 小组作业,做个什么东西好呢?',url:'/d/6801785c6a045c77606a5673425a656a2929261b',},{title:'用 AI 写爬虫,做了个爬取个人社交平台内容并分析的工具',url:'/d/1601671e6a045c77606a56724f59636a0a101739',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")