1. Introduction to the schedule library
In this lecture, we’re gonna be like clockwork masters, learning how to run tasks on a schedule, so our code works exactly when we want it to. Get ready, today we’re taming time!
The schedule
library is a lightweight module for running functions based on specified schedules. If your code needs to perform tasks at certain times, like gathering data every day at 6 AM or sending daily reminders, schedule
is your go-to.
Here are the main advantages of schedule
:
- Simplicity: An intuitive interface that’s way easier to use than the standard
cron
orthreading
solutions. - Minimalism: No unnecessary features — just what you need.
- Clarity: The use of almost natural language for scheduling tasks.
Installing the library
To get started, you’ll need to install the schedule
library. Like most Python libraries, we use pip
:
pip install schedule
Now that everything is set up, let’s dive into the world of automation and see how schedule
can change your everyday coding life.
Main methods of the schedule
library
The schedule
library provides methods for setting up tasks to run at different times and frequencies:
every().day.at("HH:MM")
— run a task every day at a specific time.every().hour
— run a task every hour.every().minute
— run a task every minute.every().week.at("HH:MM")
— run a task every week at a specific time.every().monday.at("HH:MM")
— run a task every Monday at a specific time.every(10).seconds
— run a task every 10 seconds (or any set amount of time).
2. Setting up recurring tasks
Let’s start with something simple — scheduling a function to run every hour. We’ll create a simple function that prints a greeting message and set it to run on a schedule.
Example: hourly greeting
import schedule
import time
def say_hello():
print("Hello! We’re working on time automation.")
# Schedule the say_hello function to run every hour
schedule.every().hour.do(say_hello)
while True:
# Run all pending tasks whose time has come
schedule.run_pending()
time.sleep(1)
And that’s it — your first script with schedule
! In this example, we use the method every().hour.do(say_hello)
to say "Hello!" every hour. Pretty cool, huh?
The command schedule.every().hour.do(say_hello)
doesn’t execute the say_hello
function directly; it adds it to the schedule. The actual function call happens when its time comes, triggered by the schedule.run_pending()
code.
3. Configuring more complex intervals
What if we want to run tasks not just every hour, but, say, every 10 minutes or on certain days of the week? schedule
supports most common time expressions, and you can use them with just as much ease.
Example: running a task every 10 minutes
import schedule
import time
def task():
print("This task runs every 10 minutes.")
# Schedule the task to run every 10 minutes
schedule.every(10).minutes.do(task)
while True:
schedule.run_pending() # Run all tasks whose time has come
time.sleep(1)
Example: running a task on specific days
Now let’s try scheduling a task that runs on specific days of the week, like Mondays and Wednesdays, but only at 9 AM.
import schedule
import time
def monday_wednesday_task():
print("This task runs on Mondays and Wednesdays at 9 AM.")
# Task runs on Mondays and Wednesdays at 9 AM
schedule.every().monday.at("09:00").do(monday_wednesday_task)
schedule.every().wednesday.at("09:00").do(monday_wednesday_task)
while True:
schedule.run_pending()
time.sleep(1)
As you can see, schedule
handles various time expressions, including specific days of the week and exact times. It’s super convenient for scheduling tasks synced with your workflow or planned events.
4. Handling tasks with different time conditions
If your script runs multiple tasks, each with its own schedule, schedule
can easily manage that too. For example, one task can run every morning, and another one on Fridays.
Example: combining different tasks in one script
import schedule
import time
def morning_task():
print("Good morning! Time for the morning task.")
def friday_task():
print("Yay, it’s Friday! Time for the Friday task.")
# Morning tasks every morning at 7:30
schedule.every().day.at("07:30").do(morning_task)
# Friday tasks every Friday at 4 PM
schedule.every().friday.at("16:00").do(friday_task)
while True:
schedule.run_pending()
time.sleep(1)
Practical Tips and Common Mistakes
When it comes to automating tasks with schedule
, there are a few things to keep in mind.
First, remember that schedule
runs tasks in the main thread. This means if your task is long-running or might block other tasks, you should consider using multithreading or asynchronous calls for these tasks.
Second, if the task needs to run at a precise time and it’s critical, make sure your device is on and the script is running. schedule
will not work if the script is stopped or the device is off.
Finally, a common misconception is expecting schedule
to catch up on missed tasks. Unfortunately, if your script wasn’t running when the task was supposed to execute, schedule
won’t run it retroactively.
GO TO FULL VERSION