Categories: python

Timer Objects in Python

Timer objects are used to schedule a function to be executed at a certain time in the future. The timer is started with a specified number of seconds to wait before executing the function, but the actual time when the function is executed will depend on the availability of system resources and the scheduling of other threads.

Timer objects are created using threading.Timer constructor, which takes two arguments: the number of seconds to wait before executing the function, and the function to execute.

To start a timer, you can call the start() method on the Timer object. This will start the timer and create a new thread to execute the function at the specified time.

Syntax for creating a Timer object in Python:
threading.Timer(interval, function, args=[], kwargs={})

This allows us to create a timer object that will run the function with arguments args and keyword arguments kwargs after a time interval of seconds.

Methods of Timer class:

Following are the methods used in the Timer class:

  • The start() method is used to start the timer and create a new thread to execute the function at the specified time.
  • The cancel() method is used to stop the timer and prevent the function from being executed.

For Instance,

import threading

def greeting():
  print("Hello, World!")

# Create a timer that will run the greeting function in 5 seconds
t = threading.Timer(5.0, greeting)

# Start the timer
t.start()

# Cancel the timer
t.cancel()

In this example, the timer is started with the start() method and then immediately stopped with the cancel() method.

You can also use the is_alive() method to check if the timer is currently running:

import threading

def greeting():
  print("Hello, World!")

# Create a timer that will run the greeting function in 5 seconds
t = threading.Timer(5.0, greeting)

# Start the timer
t.start()

# Check if the timer is running
if t.is_alive():
  print("Timer is running")
else:
  print("Timer is not running")

# Cancel the timer
t.cancel()

This will print “Timer is running” to the console, and then stop the timer with the cancel() method.

Note:

It’s important to note that the Timer object is not a real-time scheduling mechanism and should not be used for tasks that need to be executed with a high level of precision. Instead, it’s intended for tasks that can be executed with some degree of flexibility in their timing.

Note: also read about RLock Object in Python

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago