How to Use Python’s Schedule Library for Lightweight Task Scheduling

This guide walks through installing the schedule package, demonstrates basic and advanced usage such as setting various time intervals, canceling jobs, running multiple tasks, limiting executions, adding tags, and handling errors, all with clear Python code examples.

Subtle Storm
Subtle Storm
Subtle Storm
How to Use Python’s Schedule Library for Lightweight Task Scheduling

Installation

Install via pip:

pip install schedule

Basic usage

Bind a function to a timing rule and run the scheduler loop.

import schedule
import time

def job():
    print("任务正在执行...")

schedule.every(10).seconds.do(job)

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

Common interval settings

Supported intervals: seconds, minutes, hours, days, weeks, months.

import schedule
import time

def job():
    print("任务正在执行...")

schedule.every(1).minute.do(job)          # every minute
schedule.every().hour.do(job)             # every hour
schedule.every().day.at("09:00").do(job)   # every day at 09:00
schedule.every().monday.at("08:30").do(job) # every Monday at 08:30
schedule.every(1).month.do(job)            # every 1st of month

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

Cancel a job

import schedule
import time

def job():
    print("任务正在执行...")

job1 = schedule.every(5).seconds.do(job)
schedule.cancel_job(job1)

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

Complex interval settings

import schedule
import time

def job():
    print("任务正在执行...")

schedule.every().hour.at(":15").do(job)   # at minute 15 of each hour
schedule.every().friday.do(job)          # every Friday

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

Multiple jobs

import schedule
import time

def job1():
    print("任务 1 正在执行...")

def job2():
    print("任务 2 正在执行...")

schedule.every(3).seconds.do(job1)
schedule.every(5).seconds.do(job2)

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

Limit execution count

import schedule
import time

def job():
    print("任务正在执行...")

job1 = schedule.every(2).seconds.do(job)

for i in range(3):
    schedule.run_pending()
    time.sleep(1)

Job tags

import schedule
import time

def job():
    print("任务正在执行...")

job1 = schedule.every(3).seconds.do(job)
job1.tag('important')

for job in schedule.get_jobs():
    if 'important' in job.tags:
        schedule.cancel_job(job)

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

Error handling

import schedule
import time

def job():
    try:
        print("任务开始执行...")
        1/0  # intentional error
    except Exception as e:
        print(f"任务发生错误: {e}")

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

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

Summary

schedule is a lightweight Python scheduling library.

It supports simple intervals and complex timing rules.

Jobs can be added, cancelled, limited, tagged, and wrapped with error handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythontask schedulingerror handlingschedulecron alternativejob cancellationjob tagging
Subtle Storm
Written by

Subtle Storm

The micro era's marvels are boundlessly subtle.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.