Schedule Tasks in Python with the schedule Module

How to Schedule a Task in Python with the help of the Schedule Module

We will see task scheduling in Python and how to schedule a task in Python with examples. In this article, we will see how to schedule a task in Python.

What is Task Scheduling in Python?

Task scheduling involves automating processes by defining tasks to be executed at predetermined times or intervals. In Python, task scheduling simplifies repetitive operations by allowing developers to schedule tasks to run autonomously, enhancing productivity and efficiency.

Key Concepts:

  • Task Scheduling: Task scheduling entails automating processes by defining tasks to be executed at predetermined times or intervals.
  • Schedule Library: Python’s schedule library simplifies task scheduling with a straightforward and intuitive syntax.
  • Time-Based Scheduling: Tasks can be scheduled to run at precise times using the at() method.
  • Interval-Based Scheduling: The every() method enables tasks to be scheduled at regular intervals.
  • Task Function: Task functions are Python functions executed when scheduled tasks are triggered.

How to Schedule a Task in Python?

Below, are the examples of how to schedule a task in Python with Examples.

  • Schedule Task at Specific Time
  • Schedule Tasks at Specific Intervals
  • Python Automated Email Scheduler

To schedule tasks in Python using the schedule library, First install the schedule library, and use the following command:

pip install schedule

After installing the library, import the necessary modules (schedule and time) in your Python script using the following commands:

import schedule

import time

Schedule Task at Specific Time

In this example, in the below python code the ‘schedule’ library to schedule a daily task of printing a specific message at 10:35 AM. The code enters an infinite loop, checking for pending tasks and executing them while avoiding high CPU usage by incorporating a 1-second sleep interval

Code:

import schedule 
import time

def print_message():
    print("hello! It's time to code")
schedule.every().day().at("10:35").do(print_message)
while True:
    schedule.run_pending()
    time.sleep(1)

The message prints when the time is 10:35

Output:

hello! It's time to code

Schedule Tasks at Intervals

In this example, below the Python code, the ‘ schedule ‘ library is employed to execute a task every 5 seconds. The task, defined by the ‘print_message()’ function, prints the current time. below code runs indefinitely, checking and executing pending tasks while incorporating a 1-second sleep interval for efficiency.

import schedule 
import time

def print_message():
print("Task executed at:", time.strftime("%12:%25:%10"))

schedule.every(5).seconds.do(print_message)

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

Output:

Task executed at: 12:25:15

Task executed at: 12:25:20

Task executed at: 12:25:25

and so onn....

Python Automated Email Scheduler

In this example, in this Python code, the ‘schedule’ library is utilized to schedule tasks. the function’send_email()’ is defined to simulate sending an email, and the task is scheduled to run every day at 11:53 PM using ‘schedule.every().day.at(“23:53”).do (send_email)’. below code runs continuously, checking and executing pending tasks while ensuring efficiency with a 1-second sleep interval

import schedule
import time
def send_email:
    print("Email sent at:", time.strftime("%H:%M:%S"))
schedule.every().day.at("23:53").do(send_email)
while True:
    schedule.run_pending()
    time.sleep(1)

Output:

Email sent at:23:53:00

Features of the Schedule Module

1. Time -Based Scheduling:

  • Every X seconds: Schedule.every(10).seconds.do(task)
  • Every minute: schedule.every().minute.do(task)
  • Every hour: schedule.every().hour.do(task)
  • Every day at a specific time: schedule.every().day.at(“14:00”).do(task)

2. Day-Based Scheduling:

  • Every Monday: schedule.every().monday.do(task)
  • Every Wednesday at a specific time: schedule.every().wednesday.at(“16:30”).do(task)

3. Conditional Scheduling: You can chain conditions like this:

schedule.every().day.at("18:00").do(task)

4. Cancel a job: if you want to cancel a job, keep a reference to it:

job =  schedule.every(10).seconds.do(task1)
schedule.cancel_job(job)

 

Practical Use Cases

  1. Automated Email Reminders: send an email every day at a specific time.
  2. Data Backup: Run backup scripts at scheduled intervals.
  3. Web Scraping: Periodically scrape data from websites.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top