Timer Object in python

In this article, we will learn about Timer object in Python along with examples.

What is Timer Object in Python?

A Timer object in Python is a part of “threading” module, which enables you to schedule a function to execute after a specified time period has passed.

 

Example :

import threading 
def hello(): 
    print("Hello, world!")

timer = threading.Timer(5.0, hello)

timer.start()

In this example, the “hello” function will execute after 5 seconds.

OUTPUT :

Hello, world!

Creating a Timer Object

To create a Timer Object, you use the “threading .Timer” class. You need to specify the delay(in seconds) and the function that to be invoked when the timer expires.

Syntax  :

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

“interval” : The time in seconds before the function id called.

“function” : The function to be called.

“args” : Optional. (A list or tuple of arguments for the function)

“keywordArguments” : Optional.  (A dictionary of keyword arguments for the function)

 

CODE :

 

import threading 
def greet(name):
    print(f "Hello, {name}!") 

timer = threading.Timer(3.0, greet, args=["Charlie"])

timer.start()

 

OUTPUT : 

Hello, Charlie!

 

Explanation :

In this example, the “greet” function will be called with the argument “Charlie” after 3 seconds.

 

Cancelling a Timer Object

You might want to cancel a timer before it expires. You can achieve this by calling the “cancel” method on the Timer Object.

Syntax :

timer. cancel()

 

CODE :

 

import threading 
def goodbye():
    print("Goodbye, world!")

timer = threading.Timer(10.0, goodbye)

timer.start()

timer.cancle()

 

OUTPUT :

No output

Explanation:

In this example, you will not call the “goodbye” function because you cancel the timer before the 10 seconds passed.

Timer modules

Besides “threading.Timer”, Python has other modules and functions that deal with timing and scheduling tasks.

“time.sleep”

The “time.sleep” function pauses the execution of the current thread for a given number of seconds.

CODE :

import time 
print("Start")

time.sleep(2)
print("End")

In this example, the program will print “Start”, pause for 2 seconds, and then print “End”.

OUTPUT :

Start
End

 

Date Time module

Python’s “datetime” module offers classes for handling dates and times. It allows you to perform various operations related to date and time, such as getting the current date and time, formatting dates, performing arithmetic operations on dates, and much more.

Syntax :

class datetime. datetime(args)

args contains (year, month, day, hour, min, seconds)

CODE :

 

import datetime
date_string = "2024-06-02 14:30:00"

parsed_date = datetime.datetime.strptime(date_string, "%y-%m-%d %H:%M:%S")
print("Parsed date and time:", parsed_date)

 

OUTPUT :

Parsed sate and time: 2024-06-02  14:30:00

Also read: Unix time to datetime in Python

Leave a Reply

Your email address will not be published. Required fields are marked *