简体   繁体   中英

Make python script run everyday within certain hours

I am trying to make a python script (infinite loop) to work everyday from 9am until around 23.00 and this over and over again. I did some research and come up with this code in the end:

while True:
    if dt.now().hour in range(9, 23):  
        if __name__ == "__main__":

            """ Not important """

            while True:
                try:
                    """ bet_automation contains all the necessary code """

                    bet_automation()

                except Exception as ex:

                    """ Catch all the error of bet_automation() and restart program """

                    print_error(ex)
                    time.sleep(5)
                    if not isinstance(ex, LoginTimeoutException):
                        try:
                            driver = get_driver()
                            filename = str(time.time())
                            driver.get_screenshot_as_file("errors/{}.png".format(filename))
                            with io.open("errors/{}.html".format(filename)) as f:
                                f.write(unicode(driver.page_source))
                        except:
                            pass
                    try:
                        quit_driver()
                    except:
                        pass

    else:
        sys.exit(0)

By this, the script manages to start at 20.00 and works correctly. Even if I run it earlier, it only starts working at 20.00, which is great, but it does not terminate at 21, which is confusing.

I'm well aware that this is probably a super easy and dumb question, but as I said I'm the ultimate beginner. I had this script programmed by a "professional" programmer and I'm trying to edit it and improve it and I would like to do it myself to understand the entire process.

Every insight is highly appreciated,

Thank you very much,

:)

Your code contains two loops. First off, the outer loop. This one is basically irrelevant; if you start the program sometime between 9:00 and 23:00 the dt.now().hour in range(9, 23) will evaluate to True , so the code will enter the inner (infinite) loop. If the condition evaluates to False the program will exit. So, the outer loop body will only ever be executed once.

Then, the inner loop. This one is infinite, as soon as it is entered the code will never break out of it. If on a certain iteration bet_automation() does not throw an exception, it will be executed again during the next iteration. If on a certain iteration bet_automation() does throw an error, it will simply be caught and handled, and the loop will continue.

If you want the code to stop at some point, you need to build in a check on the current time inside the inner loop, like so:

while True:
    try:
         bet_automation()
         if dt.now().hour not in range(9, 23):
             break
(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM