Python Schedule tutorial shows how to use the schedule library for task scheduling in Python.
last modified February 15, 2025
In this article, we show how to use the schedule library in Python. The schedule library is used to schedule tasks to run at specific intervals or times. It is particularly useful for automating repetitive tasks, such as sending emails, generating reports, or performing backups.
The schedule library is easy to use and provides a simple API for scheduling tasks.
Before using the schedule library, you need to install it using pip.
$ pip install schedule
The following example demonstrates how to schedule a task to run every 10 seconds.
main.py
import schedule import time
def task(): print(“Task is running…”)
schedule.every(10).seconds.do(task)
while True: schedule.run_pending() time.sleep(1)
In this program, the schedule.every(10).seconds.do(task) function is used to schedule the task function to run every 10 seconds. The schedule.run_pending() function checks if any scheduled tasks are due to run and executes them.
$ python main.py Task is running… Task is running… Task is running…
The following example demonstrates how to schedule a task to run at a specific time every day.
main.py
import schedule import time
def task(): print(“Task is running…”)
schedule.every().day.at(“10:00”).do(task)
while True: schedule.run_pending() time.sleep(1)
In this program, the schedule.every().day.at(“10:00”).do(task) function is used to schedule the task function to run at 10:00 AM every day.
$ python main.py Task is running…
The following example demonstrates how to schedule multiple tasks at different intervals.
main.py
import schedule import time
def task1(): print(“Task 1 is running…”)
def task2(): print(“Task 2 is running…”)
schedule.every(5).seconds.do(task1)
schedule.every(10).seconds.do(task2)
while True: schedule.run_pending() time.sleep(1)
In this program, task1 is scheduled to run every 5 seconds, and task2 is scheduled to run every 10 seconds.
$ python main.py Task 1 is running… Task 1 is running… Task 2 is running… Task 1 is running…
The following example demonstrates how to cancel a scheduled task.
main.py
import schedule import time
def task(): print(“Task is running…”)
job = schedule.every(5).seconds.do(task)
start_time = time.time() while time.time() - start_time < 15: schedule.run_pending() time.sleep(1)
schedule.cancel_job(job) print(“Task canceled.”)
In this program, the schedule.cancel_job(job) function is used to cancel the scheduled task after 15 seconds.
$ python main.py Task is running… Task is running… Task is running… Task canceled.
Python Schedule - Documentation
In this article, we have shown how to use the schedule library in Python for task scheduling. The schedule library is a powerful tool for automating repetitive tasks.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.