pythonschedule

Running a python script using schedule library


My company blocks the Windows Task Scheduler (along with many other Python libraries), so I need to use the schedule library. My script imports several flat files, performs some grouping and simple calculations using dataframes, and then saves one final TXT file. I've read up on how schedule works, but how do I use it to run the entire script?

import pandas as pd
import time
import schedule

def daily():
    
    #then do all dataframe creation/manipulation, group by's, concatenations, etc...
     
schedule.every().day.at("16:30").do(daily)  

It doesn't error out, but it also doesn't do anything; it just states:

do daily() (last run: [never], next run: 2023-09-08 16:30:00


Solution

  • The do function only registers the job, but it will not actually execute it. You need to use the run_pending function to execute the job.

    import pandas as pd
    import time
    import schedule
    
    def daily():
        
        #dataframe creation/manipulation etc...
         
    schedule.every().day.at("16:30").do(daily)
    
    while True:
        schedule.run_pending()
        time.sleep(60)
    

    The interval of 60 seconds should be sufficient for your case since you are working with minute-level time accuracy and not seconds.


    run_pending()

    Run all jobs that are scheduled to run.

    Please note that it is intended behavior that run_pending() does not run missed jobs. For example, if you’ve registered a job that should run every minute and you only call run_pending() in one hour increments then your job won’t be run 60 times in between but only once.