REALLY need help with iterating a list.

Radamand radamand at gmail.com
Mon Jun 11 16:11:23 EDT 2007


On Jun 11, 1:23 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> infidel wrote:
> > How about something like this?
>
> > while serverlist:
> >     server = serverlist.pop(0)
> >     pinger = ping[server]
> >     if pinger.returncode==None:
> >         pinger.poll()
> >         serverlist.append(server)
> >     else:
> >         pingresult[server] = pinger.stdout.read()
> >         pingreturncode[server] = pinger.returncode
>
> > Basic idea:  as long as there are servers in the list, pop the first
> > one out of the list, see if it's done, and if it isn't, put it back on
> > the end of the list.
>
> here's a simple variation of that, which is a bit more efficient, and
> perhaps also a bit easier to use in the general case:
>
> while serverlist:
>      still_active = []
>      for server in serverlist:
>         pinger = ping[server]
>         if pinger.returncode is None:
>             pinger.poll()
>             still_active.append(server)
>         else:
>             pingresult[server] = pinger.stdout.read()
>             pingreturncode[server] = pinger.returncode
>      serverlist = still_active
>
> </F>

Thats an interesting approach but, if the returncode for a given
server is None say, 20 times in a row you will have append'ed that
server to the list 20 times, i suppose you could check the list to see
if its already there but thats a bit kludgey...

also, the line "pinger = ping[server]" would have to be extracted from
this loop otherwise your going to ping the same server repeatedly
until it answers...




More information about the Python-list mailing list