Python | Library Schedule

Python Methods and Functions

The schedule library is used to schedule a task at a specific time every day or on a specific day of the week. We can also set the time in 24-hour format so that when the task starts. Basically, the scheduling library maps the system time to your scheduled time. Once the scheduled time and system time match, the job function (scheduled command function) is called.

Install

 $ pip install schedule 

schedule.  Class Scheduler

  • schedule.every (interval = 1) : calls every instance of the default scheduler instance. Schedule a new periodic job.
  • schedule.run_pending () : Causes run_pending on the default scheduler instance. Run any jobs that are scheduled to run.
  • schedule.run_all (delay_seconds = 0) : calls run_all on the default scheduler instance. Run all tasks, whether they are scheduled or not.
  • schedule.idle_seconds () : calls idle_seconds on the default scheduler instance.
  • schedule.next_run () : calls next_run on the default scheduler instance. Date and time when the next job should start.
  • schedule.cancel_job (job) : Calls cancel_job on the default scheduler instance. Delete scheduled work.

schedule.Job (interval, scheduler = none) class

Periodic work used by the 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

  • at (time_str) : at(time_str) work every day at a specific time. Calling this is only valid for jobs scheduled to run every N days.

    Parameters: time_str — string in the format XX: YY. 
    Returns: called job instance

  • do (job_func, * args, ** kwargs) : specifies the job_func to be called every time a job starts. All additional arguments are passed to job_func when the job starts.

    Parameters: job_func — function to be scheduled
    Returns: called job instance

  • run () : start the job and reschedule it immediately. 
    Returns: return value returned by job_func
  • to (latest) : Schedule a job with irregular ( randomized) interval. For example, every (A) .to (B) .seconds executes a job function every N seconds, so A & lt; = N & lt; = B.

Let's see the implementation

# Library schedule imported

import schedule

import time

  
# Function 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 " )

 
# Scheduling tasks
# Geeks () is called every 10 minutes.

schedule.every ( 10 ). minutes.do (geeks)

 
# After every hour, geeks () is called.
schedule.every (). hour.do (geeks)

 
# Every day at 12:00 or 00:00 before bedtime () is called.

schedule.every (). day.at ( "00:00" ). do (bedtime)

  
# Every 5-10 minutes between work execution ()

schedule.every ( 5 ). to ( 10 ). minutes.do (work)

 
# Good_luck () is called every Monday
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 running all the time.

while True :

 

# Checks if the scheduled task is

# is pending or not

schedule.run_pending ()

  time.sleep ( 1 )

Link: https://schedule.readthedocs.io/en/stable/api.html