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.
Installation
Install via pip:
pip install scheduleBasic 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
