0

I want the below program to do two things. 1. stop at 4:00 PM every day and if I want to cancel I can do so using CTRL+C any time. How do I do that? This function program basically fetches JSON data from 11AM-4PM and writes to a MYSQL DB.

    def asyncfun(func):
    @wraps(func)
    def async_func(*args, **kwargs):
        func_hl = Thread(target=func, args=args, kwargs=kwargs)
        func_hl.daemon = True
        func_hl.start()
        return func_hl
    return async_func

def schedule(interval):
    def decorator(func):
        def periodic(scheduler, interval, action, actionargs=()):
            scheduler.enter(interval, 1, periodic,
                            (scheduler, interval, action, actionargs))
            action(*actionargs)

        @wraps(func)
        def wrap(*args, **kwargs):
            scheduler = sched.scheduler(time.time, time.sleep)
            periodic(scheduler, interval, func)
            scheduler.run()
        return wrap
    return decorator

@asyncfun
@schedule(60)
def periodic_event():
    #Error handling
    try:
        <<my code here>>
    except Exception as e:
        print(e)
        pass

if __name__ == '__main__':
    print('Start')
    print("Press CTRL+C to Stop")  //This should work
    periodic_event()
    print('End') 
    
user4815162342
  • 141,790
  • 18
  • 296
  • 355
JD Nayak
  • 21
  • 1
  • 4
  • to exit, try `except KeyboardException`. Also read up on the `datetime` module. – J'e Sep 21 '20 at 19:36
  • except KeyboardException is not working. I have tried. And since I am new tried to read a lot on datetime but couldn't understand. – JD Nayak Sep 22 '20 at 08:00
  • your code is really hard to debug because of the multiple levels if nested functions. Maybe this will help: https://stackoverflow.com/a/11436603/2601293 – J'e Sep 22 '20 at 11:23
  • Forget about scheduling and CTRL+C. Can anyone tell me just what function to write so that the threads are killed and the program is stopped? – JD Nayak Sep 23 '20 at 09:14

0 Answers0