Scheduler class in Python

Overview:

The scheduler class from Python standard library module sched facilitates the following:

  • Schedule one or more events
  • Execute the events after elapse of specified time duration

The scheduler instances are not thread safe.

How to schedule events using the scheduler class:

  • Create an instance of the scheduler class
  • Call the enter() function to enter/add events into the scheduler
  • Call the run() method on the scheduler instance

Creating a Scheduler instance:

  • A scheduler is created with two functions
    • A time function
    • A delay function

Entering events into the scheduler:

  • An event is added into the scheduler by calling the enter() function on the scheduler object with the following parameters:
    • delay – After how many seconds the event has to be executed
    • priority – As in UNIX, lower the number, higher the priority of the event. When two events are scheduled to be executed at the same time, they are executed as per their priority.
    • action – What is the event that is scheduled? What constitutes the event? It is another python function defined by the Python developer. Anything contained inside this function is executed after expiry of the delay.
    • Parameters to the above action function can be specified in any of the two parameters of the enter() function.
      • Argument – An iterable, A sequence
      • kwargs – keyword arguments as a dictionary
    • As an event is added, the enter() function returns the added event as a named tuple.
    • This namedtuple can be used for cancelling the event using the cancel() method.

 

Executing the events present in the scheduler:

  • Invoking the method run() of the scheduler instance puts the scheduler into running state.
  • As the scheduler runs, each of the events from its internal queue (were added using enter()) are executed.
  • Once executed, the events are removed from the internal queue of the event.

Example:

# Example Python program that schedules multiple events

# and executes them through the scheduler instance

import sched

import time

import queue

 

# Create a scheduler instance

myScheduler = sched.scheduler(time.time, time.sleep);

 

# A list of events as Queue - T3 will be popped first

eventQueue = ["T1","T2","T3"];

 

# The action/event function

def eventFunction(eventQueue):

    print("Executing the event:%s"%eventQueue.pop());

 

# After how many seconds event should be executed

afterNSeconds   = 5;

 

# Lower the value, higher the priority among the events scheduled at the same time

priorityValue   = 1;   

 

# Add all the tasks from the event queue to the scheduler

eventCount = len(eventQueue);

i = 0;

 

while i < eventCount:

    afterNSeconds = afterNSeconds + 5;

    regEvent = myScheduler.enter(afterNSeconds, 1, eventFunction, argument=(eventQueue,));

    print(regEvent);

    i = i + 1;

 

# Now execute the events from the scheduler

myScheduler.run();

 

Output:

Executing the event:T3

Executing the event:T2

Executing the event:T1

 


Copyright 2024 © pythontic.com