Skip to main content

Scheduling tasks with the schedule library

cron/Task Scheduler fire OS-level timers, yet quick prototypes embed schedules inside Python with schedule: fluent .every(...).seconds chains keep demos self-contained alongside lessons on **Filesystem** and **Email**.

pip install schedule

📚 Prerequisites

  • Comfortable with infinite loops guarded by graceful shutdown considerations.

🎯 What you'll master

  • Run recurring jobs inside long-lived processes.
  • Contrast embedded scheduling vs. system-level triggers.

Example supervisor loop

import time
import schedule

def compaction_job():
print("Compaction tick…")

schedule.every().day.at("01:05").do(compaction_job)
schedule.every(15).minutes.do(lambda: print("heartbeat"))

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

For production fleets, systemd or Kubernetes CronJobs supervise restarts—you usually externalize recurrence there while keeping job bodies as plain functions.


💡 Key takeaways

  • Do not silently swallow exceptions inside jobs—bubble them through logging alarms.
  • Embedding scheduling simplifies developer laptops; fleets still merit OS schedulers.

➡️ Next steps

Bundle everything into **a simple automation script** before tackling Chapter 6 testing workflows.