The Wayback Machine - https://web.archive.org/web/20240618233740/https://www.geeksforgeeks.org/timer-objects-python/
Open In App

Timer Objects in Python

Last Updated : 28 Jun, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Timer objects are used to represent actions that needs to be scheduled to run after a certain instant of time. These objects get scheduled to run on a separate thread that carries out the action. However, the interval that a timer is initialized with might not be the actual instant when the action was actually performed by the interpreter because it is the responsibility of the thread scheduler to actually schedule the thread corresponding to the timer object.

Timer is a sub class of Thread class defined in python. It is started by calling the start() function corresponding to the timer explicitly.

Creating a Timer object

Syntax:

threading.Timer(interval, function, args = None, kwargs = None) 

Create a timer that will run function with arguments args and keyword arguments kwargs, after interval seconds have passed. If args is None (the default) then an empty list will be used. If kwargs is None (the default) then an empty dict will be used.




# Program to demonstrate
# timer objects in python
  
import threading
def gfg():
    print("GeeksforGeeks\n")
  
timer = threading.Timer(2.0, gfg)
timer.start()
print("Exit\n")


Output:

Exit
GeeksforGeeks

Explanation: The program above, schedules gfg() function to run after 5 seconds of interval since start() function call.

Cancelling a timer

Syntax:

timer.cancel()

Stop the timer, and cancel the execution of the timer’s action. This will only work if the timer is still in its waiting stage.




# Program to cancel the timer
import threading
  
def gfg():
    print("GeeksforGeeks\n")
  
timer = threading.Timer(5.0, gfg)
timer.start()
print("Cancelling timer\n")
timer.cancel()
print("Exit\n")


Output:

Cancelling timer
Exit

Similar Reads

Python Program to Create a Lap Timer
In this article, we will make a simple timer to calculate lap-time intervals using Python. time: This module provides various time-related functions. It is a part of Python's standard library and does not require installation. Approach: The user needs to press ENTER to complete each lap. The timer keeps counting till CTRL+C is pressed. For each lap
2 min read
Set Countdown timer to Capture Image using Python-OpenCV
Prerequisites: Introduction to OpenCV Most of us have already captured image using countdown timer with our phones. We can do the same thing on our computer with the help of OpenCV. But here we can specify the countdown timer instead of choosing one of the specified countdown and whenever the particular key will be pressed (let’s say q) the countdo
3 min read
How To Create a Countdown Timer Using Python?
In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format ‘minutes: seconds’. We will use the time module here. Approach In this project, we will be using the time module and its sl
2 min read
Create Countdown Timer using Python-Tkinter
Prerequisites: Python GUI – tkinterPython Tkinter is a GUI programming package or built-in library. Tkinter provides the Tk GUI toolkit with a potent object-oriented interface. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.Approach Importing the module – tkinter, timeCrea
2 min read
How to show a timer on screen using arcade in Python3?
Prerequisite: Arcade library Arcade is a modern framework, which is used to make 2D video games. In this, article, you will learn how to show an on-screen timer using the arcade module of Python. Displaying a timer on screen is not tough job, just follow the below steps:- Step 1: First of all import the arcade module of Python C/C++ Code Step 2: De
2 min read
Timer Application using PyQt5
In this article we will see how we can create a timer application in PyQt5. A timer is a specialized type of clock used for measuring specific time intervals, for the given time we have to decrease the time until times become zero i.e counting downwards. GUI Implementation steps : 1. Create a push button to open pop up for getting time and set its
4 min read
PyQt5 QCalendarWidget - Setting Start Timer
In this article we will see how we can set the start timer of the QCalendarWidget. In order to do this we use startTimer method, this method calls the timerEvent after the certain interval is passed. It starts a timer and returns a timer identifier, or returns zero if it could not start a timer. In order to do this we will use startTimer method wit
2 min read
PyQt5 QCalendarWidget - Setting Timer Event
In this article we will see how we can implement the timer event for the QCalendarWidget. A timer event is sent to the calendar when the calendars startTimer interval is over. This event handler can be reimplemented in a subclass to receive calendar startTimer events which are passed in the event parameter. Below is the Calendar class code # QCalen
2 min read
PyQt5 QCalendarWidget - Killing the timer
In this article we will see how we can kill the timer of the QCalendarWidget. In order to start the timer we use startTimer method, this method calls the timerEvent after the certain interval is passed. It starts a timer and returns a timer identifier, or returns zero if it could not start a timer. In order to do this we will use killTimer method w
2 min read
Java.util.Timer Class in Java
Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time. Each task may be scheduled to run once or for a repeated number of executions. Each timer object is associated with a background thread that is responsible for the execution of all the tasks of a timer
6 min read
Article Tags :
Practice Tags :