🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The sched module in Python provides a way to schedule events for future execution. It is useful for creating simple task schedulers and is similar in functionality to the time module but allows for more precise control over the timing of events.
Table of Contents
- Introduction
- Key Classes and Methods
schedulerenterenterabscancelrunqueue
- Examples
- Scheduling a Simple Task
- Scheduling Multiple Tasks
- Canceling a Scheduled Task
- Using Priorities
- Real-World Use Case
- Conclusion
- References
Introduction
The sched module provides a class for scheduling events that run at a specified time in the future. The events can be scheduled to run at regular intervals or at specific times, and the module handles the timing and execution of these events.
Key Classes and Methods
scheduler
The scheduler class is used to create a scheduler object.
import sched
import time
s = sched.scheduler(time.time, time.sleep)
enter
Schedules an event to be executed after a specified delay.
event = s.enter(5, 1, print, argument=('Hello, World!',))
enterabs
Schedules an event to be executed at an absolute time.
event = s.enterabs(time.time() + 5, 1, print, argument=('Hello, World!',))
cancel
Cancels a scheduled event.
s.cancel(event)
run
Runs all scheduled events.
s.run()
queue
Returns a list of all scheduled events.
events = s.queue
Examples
Scheduling a Simple Task
import sched
import time
def task(name):
print(f'Task {name} executed at {time.time()}')
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 1, task, argument=('Task 1',))
print(f'Starting at {time.time()}')
s.run()
print(f'Finished at {time.time()}')
Output:
Starting at 1629319227.123456
Task Task 1 executed at 1629319232.123456
Finished at 1629319232.123456
Scheduling Multiple Tasks
import sched
import time
def task(name):
print(f'Task {name} executed at {time.time()}')
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 1, task, argument=('Task 1',))
s.enter(3, 1, task, argument=('Task 2',))
s.enter(10, 1, task, argument=('Task 3',))
print(f'Starting at {time.time()}')
s.run()
print(f'Finished at {time.time()}')
Output:
Starting at 1629319227.123456
Task Task 2 executed at 1629319230.123456
Task Task 1 executed at 1629319232.123456
Task Task 3 executed at 1629319237.123456
Finished at 1629319237.123456
Canceling a Scheduled Task
import sched
import time
def task(name):
print(f'Task {name} executed at {time.time()}')
s = sched.scheduler(time.time, time.sleep)
event1 = s.enter(5, 1, task, argument=('Task 1',))
event2 = s.enter(3, 1, task, argument=('Task 2',))
s.cancel(event1)
print(f'Starting at {time.time()}')
s.run()
print(f'Finished at {time.time()}')
Output:
Starting at 1629319227.123456
Task Task 2 executed at 1629319230.123456
Finished at 1629319230.123456
Using Priorities
import sched
import time
def task(name):
print(f'Task {name} executed at {time.time()}')
s = sched.scheduler(time.time, time.sleep)
s.enter(5, 2, task, argument=('Task 1',))
s.enter(5, 1, task, argument=('Task 2',)) # Higher priority
print(f'Starting at {time.time()}')
s.run()
print(f'Finished at {time.time()}')
Output:
Starting at 1629319227.123456
Task Task 2 executed at 1629319232.123456
Task Task 1 executed at 1629319232.123456
Finished at 1629319232.123456
Real-World Use Case
Scheduling a Repeated Task
You can use the sched module to schedule a task to run at regular intervals.
import sched
import time
def repeated_task(sc, interval, name):
print(f'Task {name} executed at {time.time()}')
sc.enter(interval, 1, repeated_task, (sc, interval, name))
s = sched.scheduler(time.time, time.sleep)
interval = 2 # seconds
s.enter(interval, 1, repeated_task, (s, interval, 'Repeated Task'))
print(f'Starting at {time.time()}')
s.run()
Output:
Starting at 1629319227.123456
Task Repeated Task executed at 1629319229.123456
Task Repeated Task executed at 1629319231.123456
Task Repeated Task executed at 1629319233.123456
...
Conclusion
The sched module in Python is a simple and effective way to schedule tasks for future execution. It provides precise control over the timing of events and supports priorities and cancellations. This module is useful for creating custom task schedulers and automating repetitive tasks.
References
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment