Python Timer start() Method with Example

Python Timer.start() Method: In this tutorial, we will learn about the start() method of Timer Class in Python with its usage, syntax, and examples. By Hritika Rajput Last updated : April 23, 2023

Python Timer.start() Method

The Timer.start() is used to start the timer. When this method is called, the timer object starts its timer, and after the given interval time has passed, the function is executed.

Module

The following module is required to use start() method:

import threading

Class

The following class is required to use start() method:

from threading import Timer

Syntax

The following is the syntax of start() method:

start()

Parameter(s)

The following are the parameter(s):

  • None

Return Value

The return type of this method is <class 'NoneType'>. The method does not return anything. It is used to start a thread of the Timer class.

Example 1: Use of Timer.start() Method in Python

# python program to explain the
# use of start() method in Timer class

import threading

def helper_function(i):
  print("Value printed=",i)

if __name__=='__main__':
    
  thread1 = threading.Timer(interval = 3, function = helper_function,args = (9,))
  print("Starting the timer object")
  print()
  
  # Starting the function after 3 seconds
  thread1.start()
  
  print("This gets printed before the helper_function as helper_function starts after 3 seconds")
  print()

Output

Starting the timer object

This gets printed before the helper_function as helper_function starts after 3 seconds

Value printed= 9

Example 2: Use of Timer.start() Method in Python

# python program to explain the
# use of start() method in Timer class

import threading

def helper_function(i):
  print("Value printed=",i)

if __name__=='__main__':
    
  thread1 = threading.Timer(interval = 3, function = helper_function,args = (9,))
  print("Starting the timer object")
  print()
  
  # Starting the function after 3 seconds
  thread1.start()
  
  print("This gets printed before the helper_function as helper_function starts after 3 seconds")
  print()
  
  # This cancels the thread when 3 seconds have not passed
  thread1.cancel()
  print("Thread1 cancelled, helper_function is not executed")

Output

Starting the timer object

This gets printed before the helper_function as helper_function starts after 3 seconds

Thread1 cancelled, helper_function is not executed



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.