2

I am running a python code to do continuous web scraping (on linux mint with Python 2.7). Due to some reasons, the code breaks down from time to time. What I have done so far is to manually re-run the code whenever an error occurred.

I want to write another python code to replace me to do this 'check status, if break, then re-run' job.

I am not sure where to start. Could anyone give me a hint?

user3768495
  • 4,077
  • 7
  • 32
  • 58
  • I have a couple of `try ` and `except` block in my code already. Since there are so many different ways of getting errors, I am not able to exhaust all possible errors. That's why I want to use a code to set myself free from manually re-run the code. – user3768495 Apr 20 '15 at 06:07

1 Answers1

3

You want something like this:

from my_script import main

restart = True
while restart:
    try:
        main()
        # This line will allow the script to end if main returns. Leave it out
        # if you want main to get restart even when it returns with no errors. 
        restart = False
    except Exception as e:
        print("An error in main: ")
        print(e.message)
        print("Restarting main...")

This requires that your script, my_script.py in this example, be set up something like this:

def foo():
    raise ValueError("An error in foo")

def main():
    print("The staring point for my script")
    foo()

if __name__ == "__main__":
    main()
Bi Rico
  • 25,283
  • 3
  • 52
  • 75
  • This is fantastic! I am using this code and it works great so far. However, I don't understand how the code works. Could you please give some brief explanations? – user3768495 Apr 20 '15 at 20:27
  • Could you be more specific about what you don't understand. You might want to just read up on [how try/except works in Python](https://docs.python.org/2/tutorial/errors.html#handling-exceptions) as a starting point. – Bi Rico Apr 20 '15 at 21:44
  • I don't understand how the foo() and main() functions work. Or more specifically, what does the `if __name__ == "__main__"` condition mean? I don't see the `name` variable is defined anywhere in the code. – user3768495 Apr 20 '15 at 21:47
  • `foo` and `main` are just functions, there is nothing special about them. I included two functions just to show that only 1 function, the entry point into the script, needs to be imported. The `if __name__ == "__main__": main()` doesn't matter here, you could leave it out. I put it in because it's commonly used in python scripts by some people. [Here is a post](http://stackoverflow.com/questions/419163/what-does-if-name-main-do) that goes into the purpose of `if __name__ == "__main__":`. – Bi Rico Apr 21 '15 at 21:52
  • Now I understand the code better. However, I found it somehow different with what I really want to do. In the code you (Bi Rico) wrote, it raise an error in the `my_script()` so the first code restart `my_script` over and and over again. What I need is to keep `my_script` run as long as possible, and when an intrinsic error occurred, then the other code can restart it. – user3768495 Apr 22 '15 at 16:46
  • The while loop will restart `main` on any Exception. I'm not sure what you mean by "intrinsic error", all errors should be Exceptions. I explicitly raise a `ValueError` to show that the code works, but you should be using your actual script instead of `my_script` (which I don't think should be raising any ValueError). If you want more help, try to identify what's not work, ie what you expect to happen and what's actually happening, otherwise it's very hard to help. – Bi Rico Apr 22 '15 at 18:42