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')