Method Name
run
Method Signature
run(blocking=True)
Parameters
Blocking – Boolean parameter that specifies whether the program should wait till all the events scheduled are executed.
Return Value
The method returns None in blocking mode. It returns the remaining time at which the next event comes up for execution.
Overview
- The run() method executes the events scheduled with a scheduler instance as per their scheduled time.
- The run() method operates in two modes:
- Blocking mode
- Non-blocking mode
- In blocking mode, the main program exits after all the events scheduled with a scheduler instance are executed.
- In non-blocking mode, the next event whose execution time is the soonest is executed and the program exits. The other events are not executed. When the next event is slated for execution is returned by the run()method.
Example – Scheduler running in blocking mode
# Example python program that runs a Python event scheduler
# in blocking mode
import sched
import time
# The function that defines an event
def evtfunction(descr):
print(descr);
# Create the scheduler instance
eventScheduler = sched.scheduler(time.time, time.sleep);
# Add events to the scheduler for execution
eventScheduler.enter(5, 1, evtfunction, ("Event id : 1",));
eventScheduler.enter(10, 1, evtfunction, ("Event id : 2",));
# Run the scheduler in blocking mode
eventScheduler.run(blocking=True);Output
Event id : 1
Event id : 2Example – Scheduler running in non-blocking mode
# Example Python program that uses scheduler.run()
# method in non-blocking mode
import sched
import time
# The action that constitutes the event
def action(msg):
print("Executing event:%s"%msg);
# Create a scheduler
scheduler = sched.scheduler(time.time, time.sleep);
# Add events to the scheduler's queue
scheduler.enter(0, 1, action, ("E1",));
scheduler.enter(2, 1, action, ("E2",));
scheduler.enter(2, 2, action, ("E3",));
nextEventTime = scheduler.run(blocking=False);
print("The next event is supposed to come in next %f seconds"%nextEventTime);
print("Program exiting after non-blocking execution of run");Output
Executing event:E1
The next event is supposed to come in next 1.999924 seconds
Program exiting after non-blocking execution of run