time - Make python script run everyday within certain hours -
i trying make python script (infinite loop) work everyday 9am until around 23.00 , on , on again. did research , come code in end:
while true: if dt.now().hour in range(9, 23): if __name__ == "__main__": """ not important """ while true: try: """ bet_automation contains necessary code """ bet_automation() except exception ex: """ catch error of bet_automation() , 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)) io.open("errors/{}.html".format(filename)) f: f.write(unicode(driver.page_source)) except: pass try: quit_driver() except: pass else: sys.exit(0) by this, script manages start @ 20.00 , works correctly. if run earlier, starts working @ 20.00, great, not terminate @ 21, confusing.
i'm aware super easy , dumb question, said i'm ultimate beginner. had script programmed "professional" programmer , i'm trying edit , improve , myself understand entire process.
every insight highly appreciated,
thank much,
:)
your code contains 2 loops. first off, outer loop. 1 irrelevant; if start program sometime between 9:00 , 23:00 dt.now().hour in range(9, 23) evaluate true, code enter inner (infinite) loop. if condition evaluates false program exit. so, outer loop body ever executed once.
then, inner loop. 1 infinite, entered code never break out of it. if on iteration bet_automation() not throw exception, executed again during next iteration. if on iteration bet_automation() does throw error, caught , handled, , loop continue.
if want code stop @ point, need build in check on current time inside inner loop, so:
while true: try: bet_automation() if dt.now().hour not in range(9, 23): break (...)
Comments
Post a Comment