Jump to content
Sign in to follow this  
Xen

for loop in python breaking early

Recommended Posts

Hi all again,

 

Back onto the python scripting.

 

I'm currently writing a watchdog script for some of our coldfusion servers.

 

I've got a for loop that seems to be processing one item and then exiting in stead of proceeding to the next item:

 

def service_check():
   #status = []
   for section in config.sections():
       #print section
       for option in config.options(section):
           #print option
           line = config.get(section,option)
           line = line.split(',')
           #print line
           for entry in line:
               site = entry.strip()
               try:
                   urllib2.urlopen(entry.strip(),'',5.0)
                   # Check on expected timeout 5 seconds
                   return True
               except urllib2.URLError:
                   restart_service(section,site)

 

Anyone see why the damn thing is doing it?

 

Regards,

Nick.

Share this post


Link to post
Share on other sites

Have you tried running in a debugger? idle python for example.

 

Your code has three nested loops, which loop is it actually dropping out of? At first though maybe the one of the functions on your config object is not actually returning a list of items. Without knowing the structure of the config object it's hard to give you a good response.

 

Could there possibly be an exception thrown that is not a URLError?

Share this post


Link to post
Share on other sites

Is it because you're returning? ...so for each line it returns. so basically the loop exits at the first time it encounters return, ie the first item.

 

not that I've ever done any python, or read any books on it, or seen any python code before :p (so could be very very wrong)

Edited by kikz

Share this post


Link to post
Share on other sites

cool :)

 

so put ur return statement outside all your for loops. if the loop that does the work gets an error and exception will be thrown anyway. though I guess you should catch it and return false ;)

Share this post


Link to post
Share on other sites

I guess the other question is what you are trying to achieve with the return statement, are you intending to break out of that loop?

 

In that case use the break keyword.

Share this post


Link to post
Share on other sites

Yep, return should end the function, right?

Breaks out of all the loops.

With python, you call a function, and can put returned values into a variable. eg:

 

x = function(data1, data2).

As soon as the def function(): block has a return, it assigns that to x, and moves on. function doesn't keep running.

 

If you want to save up a list of data, put them into a variable, or pickle them to a file, or pass them through a pipe.

Share this post


Link to post
Share on other sites

God i swear i am going to punch the face belonging to the person at work who is using all the bandwidth...

 

Sorry I've tried to reply to this a few times now.

 

It most certainly was the Return statement.

 

Thanks all for the help... all though i have a follow up coming :(

Share this post


Link to post
Share on other sites

Yep, return should end the function, right?

Breaks out of all the loops.

With python, you call a function, and can put returned values into a variable. eg:

 

x = function(data1, data2).

As soon as the def function(): block has a return, it assigns that to x, and moves on. function doesn't keep running.

 

If you want to save up a list of data, put them into a variable, or pickle them to a file, or pass them through a pipe.

Yes return will return a value from the current function(not supplying a value returns None)

 

break will break you out of the current for each block, if you have nested for each blocks it will only break you out of the current block. It does not break out of all for each blocks.

Share this post


Link to post
Share on other sites

what level does 5.0 represent in thread priority???

Thread priority?

Share this post


Link to post
Share on other sites

Thread priority?

 

i had more than one editing window open again i see, that must look pretty warped somewhere else too...

Yeah i kind of cocked my head to the side and said "Java?" with just the right inflection as i read that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×