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 usectrl+lBut, if we want to clear the screen while running a py
4 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 Install Pytz In Python?
In this article, we will explain how to install Pytz in Python, explore its features, and provide visual guidance with images. After installation, we will execute a code snippet to show its successful functionality. What is Pytz In Python?Pytz is a Python library that provides support for working wi
3 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 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 ConceptsCoroutines: Functions that we define using the async keyword. These functions allow us to
4 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 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
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
Flight-price checker using Python and Selenium
Python is a scripting language with many extended libraries and frameworks. It is used in various fields of computer science such as web development and data science. Also, Python can be used to automate some minor tasks which can be really helpful in the long run. The Python script mentioned in thi
4 min read
Personalized Task Manager in Python
In this article, we are going to create a task management software in Python. This software is going to be very useful for those who don't want to burden themselves with which task they have done and which they are left with. Being a coder we have to keep in mind which competition is going on, which
10 min read