Sched module in Python Last Updated : 06 Feb, 2020 Comments Improve Suggest changes 5 Likes Like Report 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: Python3 1== # Python program for Creating # an event scheduler import sched import time # Creating an instance of the # scheduler class scheduler = sched.scheduler(time.time, time.sleep) Basic methods available with scheduler instances 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 time priority: Priority value action: Calling function argument: A tuple of arguments Example: Python3 1== import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay of # 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay of # 2 seconds e1 = scheduler.enter(2, 1, print_event, (' 2nd', )) # executing the events scheduler.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 executed priority: The priority of the event action: The function that constitutes an event argument: Positional arguments to the event function kwargs: A dictionary of keyword arguments to the event function Example: Python3 1== # library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event x with delay of 1 second # enters queue using enterabs method e_x = scheduler.enterabs(time.time()+1, 1, print_event, argument = ("Event X", )); # executing the events scheduler.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. Python3 1== import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time and # name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # first event with delay # of 1 second e1 = scheduler.enter(1, 1, print_event, ('1 st', )) # second event with delay # of 2 seconds e2 = scheduler.enter(2, 1, print_event, (' 2nd', )) # removing 1st event from # the event queue scheduler.cancel(e1) # executing the events scheduler.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: Python3 1== import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # checking if event queue is # empty or not print(scheduler.empty()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # checking if event queue is # empty or not print(scheduler.empty()) # executing the events scheduler.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. Python3 1== # library imported import sched import time # instance is created scheduler = sched.scheduler(time.time, time.sleep) # function to print time # and name of the event def print_event(name): print('EVENT:', time.time(), name) # printing starting time print ('START:', time.time()) # event entering into queue e1 = scheduler.enter(2, 1, print_event, ('1 st', )) # printing the details of # upcoming events in event queue print(scheduler.queue) # executing the events scheduler.run() Output : START: 1580390446.8743565 [Event(time=1580390448.8743565, priority=1, action=, argument=('1 st', ), kwargs={})] EVENT: 1580390448.876916 1 st Create Quiz Comment R rakshitarora Follow 5 Improve R rakshitarora Follow 5 Improve Article Tags : Python python-modules Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like