无法从循环中的字典中删除 [英] can't delete from a dictionary in a loop
问题描述
这可能是比必要的更多信息,但这是我可以想到的最好的方式来描述这个问题,而不是太模糊。
任务:
我有一个进程列表(好吧,执行所述进程的字符串)
我想大致保留一些数字N一次运行。如果一个
终止,我想开始列表中的下一个,否则,
等待。
尝试解决方案:
使用子进程,我打开列表中的下一个可执行文件,并将其存储在字典中,并键入pid:
(循环外)
procs_dict = {}
(在while循环中)
进程= Popen(benchmark_exstring [num_started],shell = true)
procs_dict [process.pid] =进程
然后我睡了一会儿,然后循环要看的字典
终止了什么。对于已终止的每一个,我减少了一个
计数器,所以我知道下次开始有多少,然后尝试从字典中删除
的记录(因为那里因为我知道它被终止了,所以没有理由保持
投票。大概:
for ps in procs_dict:
if procs_dict [pid] .poll()!=无
#do计数器更新
del procs_dict [pid]
问题:
RuntimeError:字典在迭代期间改变了大小
所以,问题是:有没有办法解决这个问题?我知道我可以
从字典中删除/删除/并且每次都继续轮询
左右,但这看起来很草率,就像它可以保留大量内存一样/>
我不需要,因为大概是字典中持有一个对Popen对象的
引用意味着垃圾收集器永远不会被b $ b回收它。是唯一合理的解决方案来做一些像
将所有这些pid附加到列表中,然后在我迭代了
字典后,迭代列表删除pids?
(另外,从实现方面来说,有没有理由字典
迭代器无法处理?如果我可能是在
迭代器前面删除了,但是因为我正在从后面删除它...)
This might be more information than necessary, but it''s the best way I
can think of to describe the question without being too vague.
The task:
I have a list of processes (well, strings to execute said processes)
and I want to, roughly, keep some number N running at a time. If one
terminates, I want to start the next one in the list, or otherwise,
just wait.
The attempted solution:
Using subprocess, I Popen the next executable in the list, and store
it in a dictionary, with keyed on the pid:
(outside the loop)
procs_dict={}
(inside a while loop)
process = Popen(benchmark_exstring[num_started], shell=true)
procs_dict[process.pid]=process
Then I sleep for a while, then loop through the dictionary to see
what''s terminated. For each one that has terminated, I decrement a
counter so I know how many to start next time, and then try to remove
the record from the dictionary (since there''s no reason to keep
polling it since I know it''s terminated). Roughly:
for pid in procs_dict:
if procs_dict[pid].poll() != None
# do the counter updates
del procs_dict[pid]
The problem:
RuntimeError: dictionary changed size during iteration
So, the question is: is there a way around this? I know that I can
just /not/ delete from the dictionary and keep polling each time
around, but that seems sloppy and like it could keep lots of memory
around that I don''t need, since presumably the dictionary holding a
reference to the Popen object means the garbage collector could never
reclaim it. Is the only reasonable solution to do something like
append all of those pids to a list, and then after I''ve iterated over
the dictionary, iterate over the list of pids to delete?
(Also, from the implementation side, is there a reason the dictionary
iterator can''t deal with that? If I was deleting from in front of the
iterator, maybe, but since I''m deleting from behind it...)
推荐答案
Dan Upton写道:
Dan Upton wrote:
for ps in procs_dict:
if procs_dict [pid] .poll()!= None
#做柜台更新
del procs_dict [pid]
问题:
RuntimeError:字典在迭代期间改变了大小
for pid in procs_dict:
if procs_dict[pid].poll() != None
# do the counter updates
del procs_dict[pid]
The problem:
RuntimeError: dictionary changed size during iteration
我不知道在词典中使用pids的设置是否是最好的方式来获取$>
管理一个流程池......我会留下其他的s,可能更多知识,可以对此进行评论。 :-)但我可以告诉你如何解决
直接问题:
for ps in procs_dict.keys():
...
希望这会有所帮助!
- 汉斯
I don''t know if the setup with the pids in a dictionary is the best way to
manage a pool of processes... I''ll leave it others, presumably more
knowledgable, to comment on that. :-) But I can tell you how to solve the
immediate problem:
for pid in procs_dict.keys():
...
Hope this helps!
--Hans
16岁,23:28,Hans Nowak< zephyrfalcon!NO_SP ... @ gmail.comwrote:
On 16 mai, 23:28, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
Dan Upton写道:
Dan Upton wrote:
for procs_dict中的pid:
如果procs_dict [pid] .poll()!=无
#执行计数器更新
del procs_dict [pid]
for pid in procs_dict:
if procs_dict[pid].poll() != None
# do the counter updates
del procs_dict[pid]
问题:
The problem:
RuntimeError:字典在迭代期间改变了大小
RuntimeError: dictionary changed size during iteration
我不知道在词典中使用pids的设置是否是
管理一个流程池...我会留下其他人,大概更多知识,可以对此进行评论。 :-)但我可以告诉你如何解决
直接问题:
for ps in procs_dict.keys():
I don''t know if the setup with the pids in a dictionary is the best way to
manage a pool of processes... I''ll leave it others, presumably more
knowledgable, to comment on that. :-) But I can tell you how to solve the
immediate problem:
for pid in procs_dict.keys():
我担心这会做同样的事情。字典上的for循环
遍历dict键,因此两个语句都严格地相当于实际POV中的
。
I''m afraid this will do the same exact thing. A for loop on a dict
iterates over the dict keys, so both statements are strictly
equivalent from a practical POV.
>
16 mai,23:34," bruno.desthuilli ... @ gmail.com"
< bruno.desthuilli ... @ gmail.comwrote:
On 16 mai, 23:34, "bruno.desthuilli...@gmail.com"
<bruno.desthuilli...@gmail.comwrote:
16 mai,23:28,Hans Nowak< zephyrfalcon!NO_SP ... @ gmail.comwrote:
On 16 mai, 23:28, Hans Nowak <zephyrfalcon!NO_SP...@gmail.comwrote:
Dan Upton写道:
Dan Upton wrote:
for ps in procs_dict:
for pid in procs_dict:
(snip)
(snip)
for ps in procs_dict.keys():
for pid in procs_dict.keys():
我担心这会做同样的事情。字典上的for循环
遍历dict键,因此两个语句都严格地等于实际POV中的
。
I''m afraid this will do the same exact thing. A for loop on a dict
iterates over the dict keys, so both statements are strictly
equivalent from a practical POV.
哼。算了吧。我应该在发布前三思而后行 - 这将显示
在这里显然会有很大的不同。对不起噪音。
Hem. Forget it. I should think twice before posting - this will
obviously make a big difference here. Sorry for the noise.
这篇关于无法从循环中的字典中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!