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) 7 1227
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)
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.
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>
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...
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!!
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
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: aredo3604gif |
last post by:
I have coded a serie of singly linked lists in ANSI C which I have to
use.
The lists are then stored in a serie of buckets with chained hash
table technique.
In the various lists there are nodes...
|
by: Gustaf Liljegren |
last post by:
I ran into this problem today: I got an array with Account objects. I need
to iterate through this array to supplement the accounts in the array with
more data. But the compiler complains when I...
|
by: nuffnough |
last post by:
This is python 2.4.3 on WinXP under PythonWin.
I have a config file with many blank lines and many other lines that I
don't need.
read the file in, splitlines to make a list, then run a loop...
|
by: SpreadTooThin |
last post by:
I have a list and I need to do a custom sort on it...
for example:
a = #Although not necessarily in order
def cmp(i,j): #to be defined in this thread.
a.sort(cmp)
print a
|
by: Andreas Schmitt |
last post by:
I am somewhat new to C# and I ran into a problem in a small program I am
writing for teaching myself.
I am handling a list ob objects and I want to delete some of them inside
a loop like in:
...
|
by: Ken Pu |
last post by:
Hi all,
I observed an interesting yet unpleasant variable scope behaviour with
list comprehension in the following code:
print
print x
It outputs:
|
by: John Salerno |
last post by:
I know it's popular and very handy, but I'm curious if there are purists
out there who think that using something like:
for x in range(10):
#do something 10 times
is unPythonic. The reason I...
|
by: kj |
last post by:
Hi. I'd like to port a Perl function that does something I don't
know how to do in Python. (In fact, it may even be something that
is distinctly un-Pythonic!)
The original Perl function takes...
|
by: Terry Reedy |
last post by:
SUBHABRATA, I recommend you study this excellent response carefully.
castironpi wrote:
It starts with a concrete test case -- an 'executable problem
statement'. To me, this is cleared and...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |