Schedule
Schedule is a lightweight, in-process job scheduler for Python that allows you to schedule tasks to run at specific intervals or times. It aims for a human-readable syntax and is designed for simplicity. The current version is 1.2.2, and it maintains a stable, low-cadence release cycle, indicating a mature and well-tested library.
Warnings
- gotcha Jobs will only run if `schedule.run_pending()` is called. If you forget to include it in a loop or call it infrequently, scheduled jobs will not execute on time or at all.
- gotcha Schedule is an in-process scheduler and does not provide persistence. All scheduled jobs are lost when the Python script terminates. It is not designed to be a standalone service like cron.
- gotcha Long-running jobs can block the `schedule.run_pending()` call, preventing other jobs from being checked or executed on time. This can lead to scheduling delays or missed jobs.
Install
-
pip install schedule
Imports
- schedule
import schedule
Quickstart
import schedule
import time
def greet_job(name):
print(f"Hello, {name}!")
def farewell_job():
print("Goodbye!")
# Schedule a job to run every 5 seconds, passing arguments
schedule.every(5).seconds.do(greet_job, 'Alice')
# Schedule a job to run once per minute
schedule.every().minute.do(farewell_job)
print("Scheduler started. Jobs will run in the console.")
print("Press Ctrl+C to stop.")
try:
while True:
schedule.run_pending() # Run all jobs that are due
time.sleep(1) # Wait one second before checking again
except KeyboardInterrupt:
print("Scheduler stopped.")