Python sched Module

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

  1. Introduction
  2. Key Classes and Methods
    • scheduler
    • enter
    • enterabs
    • cancel
    • run
    • queue
  3. Examples
    • Scheduling a Simple Task
    • Scheduling Multiple Tasks
    • Canceling a Scheduled Task
    • Using Priorities
  4. Real-World Use Case
  5. Conclusion
  6. 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

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare