Categories We Write About

Using schedule Module in Python

The schedule module in Python is a lightweight, easy-to-use library for scheduling tasks at specific intervals. It allows you to run Python functions periodically at predefined times, making it perfect for automation, task management, or periodic data fetching.

Installing the schedule module

Before using schedule, you need to install it via pip:

bash
pip install schedule

Basic Usage

At its core, the schedule module lets you define jobs and set how often they run. You define a function containing the task and then schedule it with a time interval.

python
import schedule import time def job(): print("Task is running...") # Schedule the job every 5 seconds schedule.every(5).seconds.do(job) while True: schedule.run_pending() time.sleep(1)

How Scheduling Works

  • schedule.every(interval).unit.do(job_function) schedules the job_function to run at a fixed interval.

  • schedule.run_pending() checks if any jobs are due and runs them.

  • Typically, you keep the program running in a loop and call run_pending() frequently.

Supported Time Intervals

  • .seconds

  • .minutes

  • .hours

  • .days

  • .weeks

For example:

python
schedule.every().minute.do(job) schedule.every(10).hours.do(job) schedule.every().day.at("10:30").do(job)

Scheduling at Specific Times

You can schedule tasks to run at a specific time of day with .at().

python
def morning_task(): print("Good morning! Time to start work.") schedule.every().day.at("07:00").do(morning_task)

The time must be given in 24-hour format "HH:MM".

Chaining Schedules

You can chain intervals to customize schedules.

python
schedule.every(5).to(10).minutes.do(job)

This runs the job every 5 to 10 minutes (randomly picked interval).

Canceling Scheduled Jobs

You can cancel jobs using:

python
job_instance = schedule.every(10).seconds.do(job) schedule.cancel_job(job_instance)

Or clear all jobs:

python
schedule.clear()

Using Arguments in Scheduled Functions

You can pass arguments to the function:

python
def greet(name): print(f"Hello, {name}!") schedule.every().minute.do(greet, name="Alice")

Advanced Example: Multiple Jobs

python
def task1(): print("Task 1 executed") def task2(): print("Task 2 executed") schedule.every(1).minutes.do(task1) schedule.every(2).minutes.do(task2) while True: schedule.run_pending() time.sleep(1)

Integration Tips

  • Ensure your script runs continuously (e.g., in a daemon, or with a loop) to keep run_pending() active.

  • Combine with multithreading or async if tasks are long-running.

  • Use with logging to monitor task execution.

Summary

The schedule module simplifies task scheduling in Python with readable syntax and flexible intervals. It’s ideal for running scripts periodically without complex setups like cron jobs or external schedulers. Perfect for automation, data scraping, sending reminders, and much more.

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

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

Categories We Write About