用python线程来完成一个总是崩溃的脚本

2024-10-01 04:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为我编写的PublicationSaver()类奋斗了很多天,它有一个方法可以将xml文档作为字符串(这里没有显示)加载,然后将每个加载的字符串传递给自救出版物(自我,发布,myDirPath)。你知道吗

每次我使用它时,它会在大约25000个字符串之后崩溃,它会保存崩溃的最后一个字符串,我可以单独解析这个字符串,所以我想问题出在XML上。你知道吗

我问了here但没有答案。你知道吗

我目不转睛地看了很多,似乎我不是唯一一个有这个问题的人:here

所以,既然我真的需要完成这个任务,我就想:我能在main中用一个线程集包装所有的线程吗,这样当lxml parse抛出一个异常时,我就得到它并将一个结果发送给main以终止线程并重新启动它?你知道吗

#threading result_q = Queue.Queue() # Create the thread xmlSplitter = XmlSplitter_Thread(result_q=result_q) xmlSplitter.run(toSplit_DirPath, target_DirPath) print "Hello !!!\n" toSplitDirEmptyB=False while not toSplitDirEmptyB: splitterAlive=True while splitterAlive: sleep(120) splitterAlive=result_q.get() xmlSplitter.join() print "*** KILLED XmlSplitter_Thread !!! ***\n" if not os.listdir(toSplit_DirPath): toSplitDirEmptyB=True else: xmlSplitter.run(toSplit_DirPath, target_DirPath)

这是有效的方法吗?当我运行上面的代码时,我的意思是我从来没有得到“Hello!!”即使开始失败,xmlspllitter也会继续运行(有一个异常规则使它继续运行)。你知道吗


Tags: 方法run字符串herequeuemainresult线程
1条回答
网友
1楼 · 发布于 2024-10-01 04:52:37

可能线程失败了,它的join阻塞方法也失败了。看看here。将xml拆分为块并尝试解析块以避免内存错误。你知道吗

相关问题 更多 >