Python Timer Class | start() Method with Example

Python Timer.start() Method: Here, we are going to learn about the start() method of Timer Class in Python with its definition, syntax, and examples.
Submitted by Hritika Rajput, on May 22, 2020

Python Timer.start() Method

start() is an inbuilt method of the Timer class of the threading module in Python.

Timer class object is representative of an action that must execute only after the given time has passed. This class is a subclass of the Thread class. Start() method, here, 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:

    from threading import Timer

Syntax:

    start()

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:

# 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:

# 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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.