Sched module in Python

06 Feb, 2020
Sched module is the standard library, can be used in the creation of bots and other monitoring and automation applications. The sched module implements a generic event scheduler for running tasks at specific times. It provides similar tools like task scheduler in windows or Linux, but the main advantage is with Python’s own sched module platform differences can be ignored.
Example:
# Python program for Creating # an event scheduler    import schedimport time    # Creating an instance of the# scheduler classscheduler = sched.scheduler(time.time,                             time.sleep)
scheduler.enter(): Events can be scheduled to run after a delay, or at a specific time. To schedule them with a delay, enter() method is used.Syntax: scheduler.enter(delay, priority, action, argument=(), kwargs={})Parameters:delay: A number representing the delay timepriority: Priority valueaction: Calling functionargument: A tuple of argumentsExample:import schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # first event with delay of# 1 seconde1 = scheduler.enter(1, 1,                      print_event, ('1 st', ))  # second event with delay of# 2 secondse1 = scheduler.enter(2, 1,                      print_event, (' 2nd', ))  # executing the eventsscheduler.run()Output :START: 1580389814.152131
EVENT: 1580389815.1548214 1 st
EVENT: 1580389816.1533117  2nd

Syntax: scheduler.enter(delay, priority, action, argument=(), kwargs={})
Parameters:delay: A number representing the delay timepriority: Priority valueaction: Calling functionargument: A tuple of arguments
Example:
import schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # first event with delay of# 1 seconde1 = scheduler.enter(1, 1,                      print_event, ('1 st', ))  # second event with delay of# 2 secondse1 = scheduler.enter(2, 1,                      print_event, (' 2nd', ))  # executing the eventsscheduler.run()
Output :
START: 1580389814.152131
EVENT: 1580389815.1548214 1 st
EVENT: 1580389816.1533117  2nd

scheduler.enterabs() The enterabs() time adds an event to the internal queue of the scheduler, as the run() method of a scheduler is called, the entries in the queue of a scheduler are executed one by one.Syntax: scheduler.enterabs(time, priority, action, argument=(), kwargs={})Parameters:time: Time at which the event/action has to be executedpriority: The priority of the eventaction: The function that constitutes an eventargument: Positional arguments to the event functionkwargs: A dictionary of keyword arguments to the event functionExample:# library importedimport schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time and# name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # event x with delay of 1 second# enters queue using enterabs methode_x = scheduler.enterabs(time.time()+1, 1,                         print_event,                          argument = ("Event X", ));  # executing the eventsscheduler.run()Output :START: 1580389960.5845037
EVENT: 1580389961.5875661 Event X

Syntax: scheduler.enterabs(time, priority, action, argument=(), kwargs={})
Parameters:time: Time at which the event/action has to be executedpriority: The priority of the eventaction: The function that constitutes an eventargument: Positional arguments to the event functionkwargs: A dictionary of keyword arguments to the event function
Example:
# library importedimport schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time and# name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # event x with delay of 1 second# enters queue using enterabs methode_x = scheduler.enterabs(time.time()+1, 1,                         print_event,                          argument = ("Event X", ));  # executing the eventsscheduler.run()
Output :
START: 1580389960.5845037
EVENT: 1580389961.5875661 Event X

scheduler.cancel() Remove the event from the queue. If the event is not an event currently in the queue, this method will raise a ValueError.Syntax: scheduler.cancel(event)Parameter:event: The event that should be removed.import schedimport time    # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time and# name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # first event with delay # of 1 seconde1 = scheduler.enter(1, 1,                      print_event,                     ('1 st', ))    # second event with delay # of 2 secondse2 = scheduler.enter(2, 1,                      print_event,                     (' 2nd', ))  # removing 1st event from # the event queuescheduler.cancel(e1)  # executing the eventsscheduler.run()Output :START: 1580390119.54074
EVENT: 1580390121.5439944  2nd

Syntax: scheduler.cancel(event)
Parameter:event: The event that should be removed.
import schedimport time    # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time and# name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # first event with delay # of 1 seconde1 = scheduler.enter(1, 1,                      print_event,                     ('1 st', ))    # second event with delay # of 2 secondse2 = scheduler.enter(2, 1,                      print_event,                     (' 2nd', ))  # removing 1st event from # the event queuescheduler.cancel(e1)  # executing the eventsscheduler.run()
Output :
START: 1580390119.54074
EVENT: 1580390121.5439944  2nd

scheduler.empty() It return True if the event queue is empty. It takes no argument.Example:import schedimport time    # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # checking if event queue is# empty or notprint(scheduler.empty())  # event entering into queuee1 = scheduler.enter(2, 1,                     print_event,                     ('1 st', ))  # checking if event queue is # empty or notprint(scheduler.empty())  # executing the eventsscheduler.run()Output :START: 1580390318.1343799
True
False
EVENT: 1580390320.136075 1 st

Example:
import schedimport time    # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # checking if event queue is# empty or notprint(scheduler.empty())  # event entering into queuee1 = scheduler.enter(2, 1,                     print_event,                     ('1 st', ))  # checking if event queue is # empty or notprint(scheduler.empty())  # executing the eventsscheduler.run()
Output :
START: 1580390318.1343799
True
False
EVENT: 1580390320.136075 1 st

scheduler.queue Read-only attribute returning a list of upcoming events in the order they will be run. Each event is shown as a named tuple with the following fields: time, priority, action, argument, kwargs.# library importedimport schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # event entering into queuee1 = scheduler.enter(2, 1,                     print_event,                      ('1 st', ))  # printing the details of# upcoming events in event queueprint(scheduler.queue)  # executing the eventsscheduler.run()Output :START: 1580390446.8743565[Event(time=1580390448.8743565, priority=1, action=, argument=(‘1 st’, ), kwargs={})]EVENT: 1580390448.876916 1 st
# library importedimport schedimport time  # instance is createdscheduler = sched.scheduler(time.time,                            time.sleep)  # function to print time # and name of the eventdef print_event(name):    print('EVENT:', time.time(), name)  # printing starting timeprint ('START:', time.time())  # event entering into queuee1 = scheduler.enter(2, 1,                     print_event,                      ('1 st', ))  # printing the details of# upcoming events in event queueprint(scheduler.queue)  # executing the eventsscheduler.run()
Output :
START: 1580390446.8743565[Event(time=1580390448.8743565, priority=1, action=, argument=(‘1 st’, ), kwargs={})]EVENT: 1580390448.876916 1 st
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts
Convert integer to string in Python
Introduction To PYTHON