Open In App

Python | Schedule Library

Schedule is in-process scheduler for periodic jobs that use the builder pattern for configuration. Schedule lets you run Python functions (or any other callable) periodically at pre-determined intervals using a simple, human-friendly syntax.
Schedule Library is used to schedule a task at a particular time every day or a particular day of a week. We can also set time in 24 hours format that when a task should run. Basically, Schedule Library matches your systems time to that of scheduled time set by you. Once the scheduled time and system time matches the job function (command function that is scheduled ) is called.

Installation 



 $ pip install schedule  

schedule.Scheduler class

schedule.Job(interval, scheduler=None) class

A periodic job as used by Scheduler.
 

Parameters:
interval: A quantity of a certain time unit 
scheduler: The Scheduler instance that this job will register itself with once it has been fully configured in Job.do().



Basic methods for Schedule.job
 

Let’s see the implementation 
 




# Schedule Library imported
import schedule
import time
 
# Functions setup
def sudo_placement():
    print("Get ready for Sudo Placement at Geeksforgeeks")
 
def good_luck():
    print("Good Luck for Test")
 
def work():
    print("Study and work hard")
 
def bedtime():
    print("It is bed time go rest")
     
def geeks():
    print("Shaurya says Geeksforgeeks")
 
# Task scheduling
# After every 10mins geeks() is called.
schedule.every(10).minutes.do(geeks)
 
# After every hour geeks() is called.
schedule.every().hour.do(geeks)
 
# Every day at 12am or 00:00 time bedtime() is called.
schedule.every().day.at("00:00").do(bedtime)
 
# After every 5 to 10mins in between run work()
schedule.every(5).to(10).minutes.do(work)
 
# Every monday good_luck() is called
schedule.every().monday.do(good_luck)
 
# Every tuesday at 18:00 sudo_placement() is called
schedule.every().tuesday.at("18:00").do(sudo_placement)
 
# Loop so that the scheduling task
# keeps on running all time.
while True:
 
    # Checks whether a scheduled task
    # is pending to run or not
    schedule.run_pending()
    time.sleep(1)

  
Reference: https://schedule.readthedocs.io/en/stable/
 


Article Tags :