-1

Whenever I run the following file (and main encounters the WebDriverException exception) my program ends instead of restarting. Would anyone know why that's happening? Any help would be greatly appreciated – thank you.

from uploadToBeatstars import main
from selenium.common.exceptions import WebDriverException

try:
    main()
except WebDriverException:
    print("Dumb target error happened. Restarting program.")
    from uploadToBeatstars import driver
    driver.close()
    import sys
    import os
    os.execv(sys.executable, ['python'] + sys.argv)
4
  • are you on windows? is there any output? Commented May 22, 2022 at 16:41
  • @AnthonySottile Yes I'm on windows and no there's no output. It just prints out the print statement, stalls for like 5 seconds, then the program just stops running. Commented May 22, 2022 at 16:44
  • 1
    And is there any reason to respawn python interpreter instead of just main() in a while loop? Commented May 22, 2022 at 17:16
  • @SUTerliakov No there isn't any reason why I couldn't do something like that. How would that look like? Commented May 22, 2022 at 17:21

2 Answers 2

1

You don't need to respawn the interpreter after a failure in general, just retry in a loop:

from uploadToBeatstars import main, driver
from selenium.common.exceptions import WebDriverException

while True:
    try:
        main()
    except WebDriverException:
        print("Dumb target error happened. Restarting program.")
        driver.close()  # Not sure if it is needed, can driver be alive after an exception?
        # Try again
    else:
        break  # Stop if no exception occurred.
Sign up to request clarification or add additional context in comments.

Comments

0

on windows, the os.exec* family of functions do not operate as they do on posixlikes -- instead of replacing the current process, they spawn a new one in the background and os._exit(1)

more on this here: https://github.com/python/cpython/issues/53394

on Windows, exec() does not really replace the current process. It creates a new process (with a new pid), and exits the current one.

you're probably best to either write a loop or use some sort of process manager

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.