本文介绍了“运行时错误:字典在迭代期间改变了大小” ;好的原子能操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在极少数情况下程序崩溃(难以重现): *多个线程在一个带有dict'等的对象树上工作。项目 被添加,删除,迭代.keys()...)。这样的线程是好的 ,这个核心数据结构只能通过原子 操作来改变,因此数据结构始终是一致的。 /> 申请表。只有dicts和list上的更改操作 本身似乎在Python级别上引起问题.. *一个线程定期pickle-dump the tree到文件:In very rare cases a program crashes (hard to reproduce) : * several threads work on an object tree with dict''s etc. in it. Itemsare added, deleted, iteration over .keys() ... ). The threads are "good"in such terms, that this core data structure is changed only by atomicoperations, so that the data structure is always consistent regardingthe application. Only the change-operations on the dicts and listsitself seem to cause problems on a Python level .. * one thread periodically pickle-dumps the tree to a file: " RuntimeError:字典在迭代期间改变了大小由 ..dump(或类似的..列表已更改......)提出 我该怎么做才能获得一个稳定的pickle-dump没有上升 执行错误甚至更糟 - 腌制文件中的错误? 是copy.deepcopy( - >" cPickle.dump(copy.deepcopy(obj),f)") 原子操作,保证不会失败? 或者我可以只在RuntimeError的情况下重试几次? (对于我来说,这是多么奇怪的赌博;重试多久?) 罗伯特 PS:Zope转储线程暴露数据定期结构化。如果Zope中的ZODB 如何在其酸洗操作中处理字典/列表更改? --- Python 2.4.1(#2 ,2005年5月5日,11:32:06) [GCC 3.3.5(Debian 1:3.3.5-12)] on linux2 "RuntimeError: dictionary changed size during iteration" is raised by..dump ( or a similar "..list changed ..." ) What can I do about this to get a stable pickle-dump without risikingexecution error or even worse - errors in the pickled file ? Is a copy.deepcopy ( -> "cPickle.dump(copy.deepcopy(obj),f)" ) anatomic opertion with a guarantee to not fail? Or can I only retry several times in case of RuntimeError? (which wouldapears to me as odd gambling; retry how often?) RobertPS: Zope dumps thread exposed data structes regularly. How does the ZODBin Zope handle dict/list changes during its pickling operations?---Python 2.4.1 (#2, May 5 2005, 11:32:06)[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2推荐答案 hmm .. 我在1,2,3的: 试试: cPickle.dump(obj,f) break 除了RuntimeError,v: f.seek(0); f.truncate( 0) 同时我认为这是cPickle.dump的一个错误:它应该使用.keys() 而不是内部的自由迭代,当酸洗基本的dicts时。如果没有人反对,我会提出错误。 罗伯特 我希望这可以工作几百万年... hmm.. for i in 1,2,3:try:cPickle.dump(obj, f)breakexcept RuntimeError,v:f.seek(0);f.truncate(0)Meanwhile I think this is a bug of cPickle.dump: It should use .keys()instead of free iteration internally, when pickling elementary dicts.I''d file a bug if no objection. Robert I hope this works for some million years ... AFAICS,这是你的代码的问题。你应该使用它来锁定你的对象。这就是Threading.Lock应该为之努力的。如果你想要使用线程,你必须知道代码的哪些部分 应该是锁。 Cya, Felipe。 - " Quem excele em empregar a for?§amilitarsubjulga os ex? ?rcitos dos outros povos sem travar batalha,toma cidades fortificadas dos outros povos sem as atacar e destr?3i os estados dos outros povos sem lutas prolongadas。 Deve lutar sob o C ?? u com o prop?3sito primordial da ''preserva?§?£o''。 Desse modo suas armas n?£se se embotar?£o,e os ganhos poder?£o ser preservados。 Essa ?? a estrat ?? gia para planejar ofensivas。 - Sun Tzu,emA arte da guerra AFAICS, it''s a problem with your code. You should lock your object whileusing it. That''s what Threading.Lock is supposed to work for. If youwant to use threads, you have to know in what parts of your code thereshould be locks. Cya,Felipe. --"Quem excele em empregar a for?§a militar subjulga os ex??rcitos dosoutros povos sem travar batalha, toma cidades fortificadas dos outrospovos sem as atacar e destr?3i os estados dos outros povos sem lutasprolongadas. Deve lutar sob o C??u com o prop?3sito primordial da''preserva?§?£o''. Desse modo suas armas n?£o se embotar?£o, e os ganhospoder?£o ser preservados. Essa ?? a estrat??gia para planejar ofensivas." -- Sun Tzu, em "A arte da guerra" 这篇关于“运行时错误:字典在迭代期间改变了大小” ;好的原子能操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 10:57