Python Timer Class | cancel() Method with Example

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

Python Timer.cancel() Method

cancel() 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 Thread class. Cancel() method in this class is used to stop the timer and cancel the timer object execution. The action will be stopped only when the timer is still in the waiting area.

Module:

    from threading import Timer

Syntax:

    cancel()

Parameter(s):

  • None

Return value:

The return type of this method is <class 'NoneType'>. The method does not return anything. It is used to cancel a thread during its waiting time.

Example 1:

# python program to explain the
# use of cancel() 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

Example 2:

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

import threading
import time

def helper_function(i):
  print("Value printed=",i)
  print()
  
if __name__=='__main__':
    
  thread1 = threading.Timer(interval = 3, function = helper_function,args = (19,))
  print("Starting the timer object")
  print()
  
  # Starting the function after 3 seconds
  thread1.start()
  # Sleeping this thread for 5 seconds
  time.sleep(5)
  
  # This will not cancel the thread as 3 seconds have passed
  thread1.cancel()
  print("This time thread is not cancelled as 3 seconds have passed when cancel() method is called")

Output:

Starting the timer object

Value printed= 19

This time thread is not cancelled as 3 seconds have passed when cancel() method is called






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.