How to Schedule Tasks in Python: Simple Guide with Examples
You can schedule tasks in Python using the
schedule library, which lets you run functions at specific intervals like every minute or hour. Alternatively, you can use the built-in threading or time modules for simple delays and repeated tasks.Syntax
The schedule library uses simple commands to schedule tasks. You define a job function, then use commands like schedule.every().minute.do(job) to run it every minute. Finally, you call schedule.run_pending() in a loop to check and run due tasks.
Key parts:
schedule.every(): sets the time interval.minute,.hour,.day: specify the unit.do(job): assigns the function to runschedule.run_pending(): runs tasks that are due
python
import schedule import time def job(): print("Task is running") schedule.every().minute.do(job) while True: schedule.run_pending() time.sleep(1)
Example
This example shows how to print a message every 5 seconds using the schedule library. It demonstrates setting up the job, scheduling it, and running the scheduler loop.
python
import schedule import time def greet(): print("Hello! This task runs every 5 seconds.") schedule.every(5).seconds.do(greet) try: while True: schedule.run_pending() time.sleep(1) except KeyboardInterrupt: print("Scheduler stopped.")
Output
Hello! This task runs every 5 seconds.
Hello! This task runs every 5 seconds.
Hello! This task runs every 5 seconds.
...
Common Pitfalls
Common mistakes when scheduling tasks in Python include:
- Not calling
schedule.run_pending()regularly, so tasks never run. - Using blocking code inside the job function, which can delay other scheduled tasks.
- Forgetting to add
time.sleep()in the loop, causing high CPU usage. - Trying to schedule tasks without installing the
schedulelibrary.
Also, schedule runs in the main thread, so long tasks can block others.
python
import schedule import time def long_task(): time.sleep(10) # Blocks scheduler for 10 seconds print("Long task done") schedule.every(1).seconds.do(long_task) while True: schedule.run_pending() time.sleep(1)
Quick Reference
Here is a quick cheat sheet for common schedule commands:
| Command | Description |
|---|---|
| schedule.every().second.do(job) | Run job every second |
| schedule.every(5).seconds.do(job) | Run job every 5 seconds |
| schedule.every().minute.do(job) | Run job every minute |
| schedule.every().hour.do(job) | Run job every hour |
| schedule.every().day.at("10:30").do(job) | Run job daily at 10:30 AM |
| schedule.run_pending() | Run all jobs that are scheduled to run |
| time.sleep(1) | Pause loop to reduce CPU usage |
Key Takeaways
Use the schedule library for easy and readable task scheduling in Python.
Always call schedule.run_pending() regularly inside a loop to execute tasks.
Avoid blocking code inside scheduled functions to keep tasks running smoothly.
Add time.sleep() in your loop to prevent high CPU usage.
For simple delays, Python's time.sleep() can be used but lacks advanced scheduling features.