471,353 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

REALLY need help with iterating a list.

This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

Jun 11 '07 #1
7 1170
alg
Reverse iteration should do the trick, if I understand your problem:

for server in reversed(serverlist):
...
else:
serverlist.remove(server)

On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

Jun 11 '07 #2
On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...

================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)

while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)
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.

Jun 11 '07 #3
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>

Jun 11 '07 #4
On Jun 11, 1:23 pm, Fredrik Lundh <fred...@pythonware.comwrote:
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...

Jun 11 '07 #5
On Jun 11, 12:59 pm, infidel <saint.infi...@gmail.comwrote:
On Jun 11, 11:30 am, Radamand <radam...@gmail.comwrote:
This has been driving me buggy for 2 days, i need to be able to
iterate a list of items until none are left, without regard to which
items are removed. I'll put the relevant portions of code below,
please forgive my attrocious naming conventions.
Basically i'm trying to spin up some subprocesses that will ping a
group of servers and then wait until all of them have completed (good
or bad), store the ping result and the return code, and move on.
The problem comes in the second block, when i try to iterate the
list of servers and remove the ones that are finished, for some reason
Python appears to re-index the list when I remove an item and the next
step through the loop it cant find the item its expecting because the
indexes have changed.
Any assistance would be appreciated...
================================================== ===========================
for server in serverlist:
ping[server] = subprocess.Popen("ping -c 1 " + str(server) + " 5",
shell=True, stdout=subprocess.PIPE)
while len(serverlist) 0:
for server in serverlist:
if ping[server].returncode==None:
ping[server].poll()
else:
pingresult[server] = ping[server].stdout.read()
pingreturncode[server] = ping[server].returncode
serverlist.remove(server)

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.
I like this idea, ill try it out asap.

ok, tried it, works perfectly!! It never occurred to me to use pop to
pull one off and put it back on if it wasnt done, very nice! Thank
You!!

Jun 11 '07 #6
En Mon, 11 Jun 2007 17:11:23 -0300, Radamand <ra******@gmail.comescribió:
On Jun 11, 1:23 pm, Fredrik Lundh <fred...@pythonware.comwrote:
>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...
Read the code again and notice that there are TWO lists involved. There is
at most one append per server - unless there are duplicates in the
original list, the new list won't have any.
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...
Uh...?

--
Gabriel Genellina

Jun 11 '07 #7
Radamand wrote:
>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

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...
did you miss that there are two loops here? the inner loop loops over
the available servers, and add servers that needs to be checked again to
a *new* list. that list is then assigned to the original serverlist
variable.

</F>

Jun 11 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Gustaf Liljegren | last post: by
5 posts views Thread by nuffnough | last post: by
20 posts views Thread by SpreadTooThin | last post: by
1 post views Thread by Ken Pu | last post: by
50 posts views Thread by John Salerno | last post: by
10 posts views Thread by kj | last post: by
2 posts views Thread by Terry Reedy | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.