简体   繁体   中英

How to fix APscheduler?

I am trying to run a function every 24 hours and update database. Before doing that I've read the documentation on APscheduler and tried it on a simple job

def print_date_time():
  print(time.strftime("%A, %d. %B %Y %I:%M:%S %p"))
def testing_schedule():
    scheduler = BackgroundScheduler()
    scheduler.add_job(func=print_date_time, trigger="interval", seconds=5)
    scheduler.start()

    pass

testing_schedule()

From my understanding every 5 seconds print_date_time function will run printing current time and keep on going until I close it.

What exactly is the problem?

In APScheduler you need first to start the scheduler, then to add jobs. You also need a while loop with a BackGround scheduler to keep the process alive. So:

scheduler.start()
scheduler.add_job(func=print_date_time, trigger="interval", seconds=5)
while True:
    time.sleep(1)

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