Restarting a program after exception

前端 未结 4 766
执念已碎
执念已碎 2020-12-28 08:45

I have a program that queries an API every few seconds. Each response triggers a few functions which themselves make some calls to websites and such -- calls that I don\'t w

4条回答
  •  长情又很酷
    2020-12-28 09:10

    Refactor this - you'll get a stackoverflow error sooner or later if you have enough failures.

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

    Wrap in something that looks like this, your true queryRepeatedly function?

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

    All looping, no recursion needed.

    Note that you must think carefully about how much of your program you need to restart. From your question it sounded like your actual problem was ensuring the query could try again if it sporadically fails, which is what my solution ensures. But if you want to clean up program resources - say, bounce SQL connections, which may have broken - then you need to think more carefully about how much of your program you need to "restart." In general you need to understand why your query failed to know what to fix, and in the extreme case, the right thing to do is an email or SMS to someone on call who can inspect the situation and write an appropriate patch or fix.

提交回复
热议问题