DEV Community

Cover image for Python Scheduler
Ashutosh Sharma
Ashutosh Sharma

Posted on

Python Scheduler

A scheduler

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode

Checkout the working demo at
https://replit.com/@AshutoshSharm35/Python-Scheduler-Demo?v=1

Working Demo

Latest comments (6)

Collapse
 
naveennamani profile image
naveennamani

For those of you who don't know, this package is developed by realpython.com owner dan bader

Collapse
 
vulcanwm profile image
Medea

This is really cool!
Also omg replit

Collapse
 
ashusharmatech profile image
Ashutosh Sharma • Edited

πŸ˜€

Collapse
 
vulcanwm profile image
Medea

you could try submitting that in the replit template jam

Thread Thread
 
ashusharmatech profile image
Ashutosh Sharma

@vulcanwm thanks for suggestion, Done :)

replit.com/@AshutoshSharm35/Python...

Thread Thread
 
vulcanwm profile image
Medea

yay! the best thing about this is that you can submit as many templates as you want