Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python
can't delete from a dictionary in a loop
 

Index | Next | Previous | View Flat


upton at virginia

May 16, 2008, 2:22 PM


Views: 332
Permalink
can't delete from a dictionary in a loop

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...)
--
http://mail.python.org/mailman/listinfo/python-list

Subject User Time
can't delete from a dictionary in a loop upton at virginia May 16, 2008, 2:22 PM
    Re: can't delete from a dictionary in a loop zephyrfalcon!NO_SPAM! at gmail May 16, 2008, 2:28 PM
        Re: can't delete from a dictionary in a loop bruno.desthuilliers at gmail May 16, 2008, 2:34 PM
    Re: can't delete from a dictionary in a loop gherron at islandtraining May 16, 2008, 2:33 PM
    Re: can't delete from a dictionary in a loop bruno.desthuilliers at gmail May 16, 2008, 2:38 PM
    Re: can't delete from a dictionary in a loop gherron at islandtraining May 16, 2008, 2:40 PM
        Re: can't delete from a dictionary in a loop eduardo.padoan at gmail May 16, 2008, 3:38 PM
    Re: can't delete from a dictionary in a loop zephyrfalcon!NO_SPAM! at gmail May 16, 2008, 2:51 PM
        Re: can't delete from a dictionary in a loop castironpi at gmail May 16, 2008, 3:19 PM
    Re: can't delete from a dictionary in a loop tjreedy at udel May 16, 2008, 4:46 PM
    Re: can't delete from a dictionary in a loop google at mrabarnett May 16, 2008, 6:21 PM
    Re: can't delete from a dictionary in a loop george.sakkis at gmail May 17, 2008, 1:06 AM
    Re: can't delete from a dictionary in a loop castironpi at gmail May 17, 2008, 1:33 AM
    Re: can't delete from a dictionary in a loop Scott.Daniels at Acm May 17, 2008, 9:25 PM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.