How to Schedule a Task in Python?
Last Updated :
15 Mar, 2024
We will see what is task scheduling in Python and how to schedule a task in Python with examples. In this article, we will see how we can schedule a task in Python.
What is Task Scheduling in Python?
Task scheduling involves automating processes by defining tasks to be executed at predetermined times or intervals. In Python, task scheduling simplifies repetitive operations by allowing developers to schedule tasks to run autonomously, enhancing productivity and efficiency.
Key Concepts:
- Task Scheduling: Task scheduling entails automating processes by defining tasks to be executed at predetermined times or intervals.
- schedule Library: Python's
schedule
library simplifies task scheduling with a straightforward and intuitive syntax. - Time-Based Scheduling: Tasks can be scheduled to run at precise times using the
at()
method. - Interval-Based Scheduling: The
every()
method enables tasks to be scheduled at regular intervals. - Task Functions: Task functions are Python functions executed when scheduled tasks are triggered.
How to Schedule a Task in Python?
Below, are the examples of how to schedule a task In Python With Examples.
- Schedule Task at Specific Time
- Schedule Task at Specific Intervals
- Python Automated Email Scheduler
To schedule tasks in Python using the schedule
library, First install the schedule
library, use the following command:
pip install schedule
After installing the library, import the necessary modules (schedule
and time
) in your Python script using the following commands:
import schedule
import time
Schedule Task at Specific Time
In this example, in below Python code the `schedule` library to schedule a daily task of printing a specific message at 10:35 AM. The code enters an infinite loop, checking for pending tasks and executing them while avoiding high CPU usage by incorporating a 1-second sleep interval.
Python3
import schedule
import time
# Define a function to print a message
def print_message():
print("Hello! It's time to code on GFG")
# Schedule the task to run every day at 7:00 AM
schedule.every().day.at("10:35").do(print_message)
while True:
schedule.run_pending()
time.sleep(1)
Output

Schedule Task at Intervals
In this example, in below Python code, the `schedule` library is employed to execute a task every 5 seconds. The task, defined by the `print_message()` function, prints the current time. below code runs indefinitely, checking and executing pending tasks while incorporating a 1-second sleep interval for efficiency.
Python3
import schedule
import time
def print_message():
print("Task executed at:", time.strftime("%H:%M:%S"))
# Schedule task to run every 5 seconds
schedule.every(5).seconds.do(print_message)
# Keep the program running to allow scheduled tasks to execute
while True:
schedule.run_pending()
time.sleep(1)
Output

Python Automated Email Scheduler
In this example, In this Python code, the `schedule` library is utilized to schedule a task. The function `send_email()` is defined to simulate sending an email, and the task is scheduled to run every day at 11:53 PM using `schedule.every().day.at("23:53").do(send_email)`. below code runs continuously, checking and executing pending tasks while ensuring efficiency with a 1-second sleep interval.
Python3
import schedule
import time
# Define a function to send email
def send_email():
print("Email sent at:", time.strftime("%H:%M:%S"))
# Schedule the task to run every day at 11:53 pm
schedule.every().day.at("23:53").do(send_email)
# Keep the program running to allow scheduled tasks to execute
while True:
schedule.run_pending()
time.sleep(1)
Output
email_scheduling
Similar Reads
How to clear screen in python?
When working in the Python interactive shell or terminal (not a console), the screen can quickly become cluttered with output. To keep things organized, you might want to clear the screen. In an interactive shell/terminal, we can simply use ctrl+l But, if we want to clear the screen while running a
5 min read
Schedule a Python Script on PythonAnywhere
Keeping the computer on 24/7 is not practical, so if you want to execute a Python script at a particular time every day, you probably need a computer that is ON all the time. To make this possible, a website PythonAnywhere gives you access to such a 24/7 computer. You can upload a Python script and
2 min read
How to make a Twitter Bot in Python?
Twitter is an American microblogging and social networking service on which users post and interact with messages known as "tweets". In this article we will make a Twitter Bot using Python. Python as well as Javascript can be used to develop an automatic Twitter bot that can do many tasks by its own
3 min read
How Use Linux Command In Python Using System.Os
Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
Autorun a Python script on windows startup
Adding a Python script to windows start-up basically means the python script will run as the windows boots up. This can be done by two step process - Step #1: Adding script to windows Startup folder After the windows boots up it runs (equivalent to double-clicking) all the application present in its
2 min read
How to make a Python program wait?
The goal is to make a Python program pause or wait for a specified amount of time during its execution. For example, you might want to print a message, but only after a 3-second delay. This delay could be useful in scenarios where you need to allow time for other processes or events to occur before
2 min read
How to Run Two Async Functions Forever - Python
In Python, asynchronous programming allows us to run multiple tasks concurrently without blocking the main program. The most common way to handle async tasks in Python is through the asyncio library. Key Concepts Coroutines: Functions that we define using the async keyword. These functions allow us
4 min read
How to Parallelize a While loop in Python?
Parallelizing a while loop in Python involves distributing the iterations of a loop across multiple processing units such as the CPU cores or computing nodes to execute them concurrently. This can significantly reduce the overall execution time of the loop, especially for tasks that are CPU-bound or
2 min read
How to Run Another Python script with Arguments in Python
Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article,
3 min read
Python Script to Shutdown Computer
As we know, Python is a popular scripting language because of its versatile features. In this article, we will write a Python script to shutdown a computer. To shut down the computer/PC/laptop by using a Python script, you have to use the os.system() function with the code "shutdown /s /t 1" . Note:
1 min read