Ben Chuanlong Du's Blog

It is never too late to learn.

Schdule Jobs in Python

Tips and Traps

  1. If you schedule a job $J_1$ using schedule in IPython shell, and then interrupt the following loop that checks and runs pending jobs,
    while True:
         schedule.run_pending()
    
    the scheduled job $J_1$ is still there unless you stop the current IPython shell. The effective consequence of this is that if you schedule another job $J_2$, and start to run it using the following loop,
    while True:
         schedule.run_pending()
    
    the job $J_1$ will also continue to run! This might not what you expect since when you intterup the loop that checks and runs pending jobs, you might thought that you killed scheduled jobs, which is not correct.

Installation

In [ ]:
pip3 install schedule

Example

In [ ]:
import schedule
import time


def job():
    print("I'm working...")


schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
schedule.every().monday.do(job)
schedule.every().wednesday.at("13:15").do(job)
schedule.every().minute.at(":17").do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

schedule.job.do

Specifies the job_func that should be called every time the job runs. Any additional arguments are passed on to job_func when the job runs.

In [ ]:
 

Comments