is used to run the function periodically using different types of patterns and syntaxes. Normally, if a task has to be performed every time after a specific time difference, a scheduler can be used.
Suppose you have a report which needs to be generated at 10 AM daily, this process can be triggered using a scheduled program.
Python has a “schedule” library that can help you perform this scheduling using very friendly syntaxes.
Official documentation of this library is available at https://schedule.readthedocs.io/en/stable/
To use this library you will need to install it using
pip install schedule
Now you are ready to schedule the jobs.
Let's try a simple example
import schedule import time # This method is the actual processing that we want to accomplish # with every scheduled execution def job(): print("Job ran") # This defines the frequency to run the program schedule.every().hour.do(job) # while loop to keep the program running in the memory while True: # it will check if any execution is pending, # if yes it will start execution schedule.run_pending() # program will sleep for 1 second and then will again start while loop time.sleep(1)
The above program will run every hour and print the line “Job ran”.
Let's look at some more examples to schedule job syntaxes
Scheudled job with frequency
# Run job every 15 seconds schedule.every(15).seconds.do(job) # Run job every 15 minutes schedule.every(32).minutes.do(job) # Run job every 12 hours schedule.every(12).hours.do(job) # Run job every 5 days schedule.every(5).days.do(job) # Run job every 2 weeks schedule.every(2).weeks.do(job)
Scheduled job for specific time
# Run job every minute at the 45th second schedule.every().minute.at(":45").do(job) # Run job every hour at the 10th minute schedule.every().hour.at(":10").do(job) # Run job every day at specific time # Everyday at 11 Hour 11 Minutes schedule.every().day.at("11:11").do(job) # Everyday at 11 Hour 11 Minutes 11 Second schedule.every().day.at("11:11:11").do(job)
Checkout the working demo at
Top comments (6)
This is really cool!
Also omg replit
you could try submitting that in the replit template jam
@vulcanwm thanks for suggestion, Done :)
yay! the best thing about this is that you can submit as many templates as you want
For those of you who don't know, this package is developed by realpython.com owner dan bader