5

I'm looking for a way to handle timeout exceptions for my Reddit bot which uses PRAW (Python). It times out at least once every day, and it has a variable coded in so I have to update the variable and then manually run the bot again. I am looking for a way to automatically handle these exceptions. I looked into try: and except:, but I am afraid that adding a break point after time.sleep(10) would stop the loop completely. I want it to keep running the loop regardless if it times out or not. There is a sample of the code below.

def run_bot():
   # Arbitrary Bot Code Here

   # This is at the bottom of the code, and it runs the above arbitrary code every 10 seconds
while True:
    try:
        run_bot()
        time.sleep(10)
    except:
        # Don't know what goes here
chepner
  • 497,756
  • 71
  • 530
  • 681
Matt T.
  • 144
  • 1
  • 1
  • 9

2 Answers2

3

Moving the sleep to finally will solve your issue, I guess. finally block will run irrespective of whether there is an exception happened or not.

def run_bot():
   # Arbitrary Bot Code Here

   # This is at the bottom of the code, and it runs the above arbitrary code every 10 seconds
while True:
    try:
        run_bot()
    except:
        from traceback import format_exc
        print "Exception happened:\n%s" % (format_exc())
    finally:
        time.sleep(10)
thiruvenkadam
  • 4,170
  • 4
  • 27
  • 26
2

It depends on what you want to do when a timeout occurs.

you can make a pass to do nothing and continue with the loop.

try:
    run_bot()
except:
    pass

In your case it would be better to write it explicitly as

try:
    run_bot()
except:
    continue

But you can also add some logging to the except clause

try:
    run_bot()
except e:
    print 'Loading failed due to Timeout'
    print e

To make sure the loop always sleeps you can do the following:

nr_of_comments = 0

def run_bot():
    # do stuff
    nr_of_comments =+ 1

while True:
    sleep(10)
    try:
        run_bot()
    except e:
        continue
Alu
  • 727
  • 5
  • 16
  • I should have added that one of the functions of the bot is that when it hasn't detected a comment for a while is sleeps for 10 seconds and then checks again, so that it doesn't keep spamming Reddit. If I were to do except: continue, would the loop still run as if it were try: run_bot()? Is there any way to keep that sleep in there? – Matt T. Sep 16 '16 at 13:57
  • I try to understand: The `run_bot` itself sleeps for 10 seconds if nothing new is found. And you sleep again after `run_bot` finished? So when exactly is the exception thrown? – Alu Sep 16 '16 at 14:10
  • I believe the exception is thrown when it tries to connect to Reddit but it fails. The sleep after the run_bot(10) is the only sleep – Matt T. Sep 16 '16 at 14:15
  • Then i would put the sleep before of the `run_bot` and continue in the except. So the loop will sleep for 10s and runs the bot. If an error occurs, the loop starts again... You can also put the sleep outside of the try-except to make sure, the loop will always sleep for 10s – Alu Sep 16 '16 at 14:24
  • Sorry for all the questions but will doing this make the program reset the variables? For example i declare variable = 10, and every time it sees a comment it adds 1 to variable. So if eventually variable = 14, and it catches the exception, will the variable still be 14? – Matt T. Sep 16 '16 at 14:45
  • it depends where you declare your variables. It they are in the same scope as the while loop, they will persist. If they are in the scope of the run_bot method, they will be overridden every time they are called. – Alu Sep 16 '16 at 14:53
  • The variable is declared and defined right before def run_bot() – Matt T. Sep 16 '16 at 14:57
  • Bot has been running beautifully for 2 days now, thanks for your answer! – Matt T. Sep 18 '16 at 18:21