Restarting the program after an exception

I have a program that requests an API every few seconds. Each answer calls up several functions that make some calls to websites themselves and those that I donโ€™t want to trust blindly in order to succeed. If I catch an exception in foo() , for example, or even in a function that calls foo() , can I completely restart the program in the exception block? Essentially, I want to call queryRepeatedly() on an exception in one of my subfunctions, without storing the previous call on the stack.

Of course, I could return the marker values โ€‹โ€‹and solve this other way, but the program is structured in such a way that the above approach seems a lot easier and cleaner.

 # Sample "main" function that I want to call def queryRepeatedly(): while True: foo() bar() baz() time.sleep(15) def foo(): # do something try: foo2() # makes a urllib2 call that I don't trust except: #restart queryRepeatedly queryRepeatedly() 

Any suggestions are welcome! Thanks.

+6
source share
3 answers

To restart something, just use the while outside of try . For instance:

 def foo(): while True: try: foo2() except: pass else: break 

And if you want to throw an exception in a chain, just do it in an external function instead of an internal function:

 def queryRepeatedly(): while True: while True: try: foo() bar() baz() except: pass else: break time.sleep(15) def foo(): foo2() 

All that indentation is a little hard to read, but easy to reorganize is:

 def queryAttempt() foo() bar() baz() def queryOnce(): while True: try: queryAttempt() except: pass else: break def queryRepeatedly(): while True: queryOnce() time.sleep(15) 

But if you think about it, you can also combine the two while into one. Using continue can be a bit confusing, but see if you like it:

 def queryRepeatedly(): while True: try: foo() bar() baz() except: continue() time.sleep(15) 
+12
source

Repair it - you will get a stackoverflow error sooner or later if you have enough failures.

queryRepeatedly should be query . It should return void and throw exceptions on failures.

Wrap what looks like your true queryRepeatedly function?

 while True: try: query() except: #handle time.sleep(15) 

The whole cycle, without recursion.

Please note that you should carefully consider how much your program needs to restart. From your question, it sounded as if your actual problem was that the request could retry if it sporadically fails, and that is what provides my solution. But if you want to clean up software resources โ€” say, abandon SQL connections that might break โ€” then you need to think more carefully about what part of your program you need to โ€œreloadโ€. In general, you need to understand why your request could not understand what to fix, and in the worst case, the right thing is to send an email or SMS to someone on call who can check the situation and write the appropriate patch or fix it.

+5
source

In your exception, make a recursive call

 except: queryRepeatedly() 
-4
source

Source: https://habr.com/ru/post/948968/


All Articles