{"id":3799,"date":"2020-08-02T10:19:31","date_gmt":"2020-08-02T04:49:31","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3799"},"modified":"2021-05-23T11:27:36","modified_gmt":"2021-05-23T05:57:36","slug":"python-timer","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-timer\/","title":{"rendered":"Understanding the Python Timer Class with Examples"},"content":{"rendered":"\n<p>Python has thousands of modules and libraries to satisfy the coder&#8217;s demands. However, to make them useful, you have to understand them first and then apply them. Without prior knowledge of certain modules, it&#8217;s uncertain what you want to do and what the module actually does. Time is one of the important factors in coding problems. All the codes are meant to reduce human work by using their powerful algorithms. Time management creates a great impact on the analysis of the performance of your code. That&#8217;s why today, we will learn in-depth about how the timer works in Python.<\/p>\n\n\n\n<p><strong>Python Timer is a class\/library to manage the time complexity of your code. Using multiple time modules, you can create a system in your code to check the time taken by the respective code snippet. There are different types of timer implementations in python according to user needs, namely, python timer function (to check script execution time), python threading timer (to check the time taken by a thread to finish), python countdown timer (create a countdown timer) and basic python time module (to help with other activities).<\/strong><\/p>\n\n\n\n<p>All of these timer implementation functions differently and focuses on providing different use to the end-user. In this post, we&#8217;ll go through every timer implementation in detail. You can use the Table of Contents to jump to the respective python timer implementation.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Basic_Python_Timer_Functions\" >Basic Python Timer Functions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Function_timetime\" >Function time.time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Function_timeThread_time\" >Function time.Thread_time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Function_timeProcess_time\" >Function time.Process_time<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Function_timePerf_counter\" >Function time.Perf_counter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Function_timemonotonic\" >Function time.monotonic<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Custom_Python_Timer_Classes\" >Custom Python Timer Classes<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Python_Timer_using_Threading\" >Python Timer using Threading<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Syntax_of_Timer_Class_in_Python\" >Syntax of Timer Class in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Parameters\" >Parameters-&nbsp;&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Return_Type\" >Return Type-&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Methods_in_Timer_class\" >Methods in Timer class&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Creating_a_Timer_Object\" >Creating a Timer Object&nbsp;<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Countdown_Timer_in_python\" >Countdown Timer in python<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Using_the_time_module\" >Using the time module<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Using_the_python_Timer_Class\" >Using the python Timer Class<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Python_Timers_as_Context_Managers\" >Python Timers as Context Managers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Python_Timer_Decorators\" >Python Timer Decorators<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Python_Timer_Cooldown\" >Python Timer Cooldown<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Other_Python_Timer_Modules\" >Other Python Timer Modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Must_Read\" >Must Read:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-basic-python-timer-functions\"><span class=\"ez-toc-section\" id=\"Basic_Python_Timer_Functions\"><\/span>Basic Python Timer Functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The most important module in python regarding time is <a href=\"https:\/\/docs.python.org\/3\/library\/time.html\">time<\/a>. <\/strong>This module contains all the basic functions to check the time and analyze it. This analysis will help you to understand the<strong> performance of your code and its efficiency<\/strong>. We&#8217;ll go through each of the important functions from this module along with its examples.<\/p>\n\n\n\n<p>Following are the basic Python Timer functions using the time module &#8211; <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/library\/time.html#time.time\">time.time()<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/time.html#time.thread_time\">time.thread_time()<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/time.html#time.process_time\">time.process_time()<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/time.html#time.perf_counter\">time.perf_counter()<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/time.html#time.monotonic\">time.monotonic()<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-function-time-time\"><span class=\"ez-toc-section\" id=\"Function_timetime\"><\/span>Function time.time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>time.time()<\/code> returns the<strong> time in seconds (float) after the epoch<\/strong>. Generally, the <strong>epoch is set to January 1, 1970, 00:00:00 (UTC)<\/strong>, and the <strong>number of seconds after this epoch is returned<\/strong>. This function depends on the computer time for calculating the number of seconds. If you change the computer time between the python execution, you can get weird numbers using this function.<\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\nstart = time.time() #start time\n\nfor i in range(1000000):\n    pass\n\nend = time.time()\nprint(&quot;Elapsed time is  {}&quot;.format(end-start))\n<\/pre><\/div>\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>Firstly, we start with importing the time module. This module contains all the <strong>basic time functions<\/strong> which we&#8217;ll use in this section. At the start of the code, we&#8217;ll declare the start variable as<code> time.time()<\/code>. This will store the<strong> current time of CPU in float number <\/strong>from the epoch. We&#8217;ll use this start variable as a reference point to measure the time. The next part contains all the codes you want to use (In this example, we&#8217;ve used a for loops).<strong> Similarly, record the end time of the CPU (Default time) and check the total execution time by using end &#8211; start.<\/strong><\/p>\n\n\n\n<p>This will print the time taken by the code in seconds. With the use of this technique, you can check the time taken by your code. T<strong>he only problem with this method is that the CPU time can be changed while the code is running. <\/strong>This will result in problematic behavior in python timer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-function-time-thread_time\"><span class=\"ez-toc-section\" id=\"Function_timeThread_time\"><\/span>Function time.Thread_time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>time.thread_time()<\/code> returns the <strong>sum of system and CPU time (float) in the current running thread<\/strong>. Moreover, it doesn&#8217;t include the time you spend in <code>time.sleep()<\/code> function. As the function is thread-specific, you can use this function to record the time differences as long as the<strong> time references belong to the same thread<\/strong>.<\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\nimport threading\nend = None\nstart = None\ndef hello():\n    global start, end\n    start = time.thread_time()\n    x = 0 \n    while x &lt; 10000000:\n        pass\n        x += 1\n    end = time.thread_time()\n\nt = threading.Thread(target = hello, args = ())\nt.start() \nt.join()\n\nprint(&quot;The time spent is {}&quot;.format(end - start))\n<\/pre><\/div>\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>As the thread_time returns thread-specific time, we have to make sure to use references in the <strong>same thread<\/strong>. For this purpose, we&#8217;ve instantiated the thread of <strong>function &#8216;hello<\/strong>&#8216; and calculated the time taken by the thread inside it. However, make sure that you cannot get the current time by calling <strong>thread_time() <\/strong>because it returns a fractional value.<strong> But you can definitely check the difference between two consecutive references as long as they belong in the same thread.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-function-time-process_time\"><span class=\"ez-toc-section\" id=\"Function_timeProcess_time\"><\/span>Function time.Process_time<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><code>time.process_time() <\/code>returns the time reference in <strong>fractional seconds (float) of the sum of system time and CPU time of current progress<\/strong>. Same as thread_time, this function doesn&#8217;t measure the time spent in <code>time.sleep()<\/code> function. Moreover, this function creates a reference based on the process. A<strong>s a result of this, only the time difference between two consecutive references will make sense.<\/strong><\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom time import process_time, sleep\n\niterations = 100000\n\nstart = process_time()  \n   \nfor i in range(iterations):\n    print(i, end=&quot; &quot;)\n  \n# Stop the stopwatch \/ counter \nend = process_time()\nprint(end, start)\nprint(&quot;Elapsed time in seconds:&quot;, end-start)\n<\/pre><\/div>\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>We start with importing the process_time function and record the start and end time between the code. The only difference between legacy <code>time.time()<\/code> and <code>time.proces_time()<\/code> is that the <strong>processing time records the time references of the current process<\/strong>, whereas <code>time()<\/code> records the system&#8217;s absolute time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-function-time-perf_counter\"><span class=\"ez-toc-section\" id=\"Function_timePerf_counter\"><\/span>Function time.Perf_counter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Perf Counter stands for<strong> Performance Counter<\/strong>. This function returns the <strong>high-resolution value of the time<\/strong>, which is<strong> valid for a short period of time<\/strong>. This function is used to get the <strong>precise time count between two references<\/strong>. As other python timer functions don&#8217;t include sleep time, <code>perf_counter<\/code> also doesn&#8217;t include it. Let&#8217;s move to an example &#8211; <\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom time import perf_counter, sleep\n  \n# integer input from user, 2 input in single line\nn = 3\n  \n# Start the stopwatch \/ counter \nstart = perf_counter()  \n  \nfor i in range(n): \n    sleep(1)\n\nend = perf_counter() \n  \n  \nprint(&quot;Elapsed time in seconds:&quot;, end-start) \n<\/pre><\/div>\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>You can use the perf counter in the same way as the process timer or thread timer. The only difference is, perf counter will return a high precise value of the time elapsed. But make sure that you use this<strong> between small processes<\/strong>, as it uses <strong>high precision<\/strong>. We&#8217;ve used a simple loop to check the time taken by it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-function-time-monotonic\"><span class=\"ez-toc-section\" id=\"Function_timemonotonic\"><\/span>Function time.monotonic<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Monotonic is a <strong>python timer that cant go backward<\/strong>. When executing a python script, the time can be changed by the user and can make a huge difference in the implementation of the timer in python. <strong>But monotonic timer ensures that the time references adjust to the external changes.<\/strong><\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom time import monotonic, sleep\n  \n# integer input from user, 2 input in single line\nn = 3\n  \n# Start the stopwatch \/ counter \nstart = monotonic()  \n  \nfor i in range(n): \n    sleep(1)\n\nend = monotonic() \n  \n  \nprint(&quot;Elapsed time in seconds:&quot;, end-start) \n<\/pre><\/div>\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>We first import the<strong> monotonic function<\/strong> from the time module. Then create<strong> two references named start and end<\/strong> in the top and bottom side of the code. This will measure the<strong> time between the two references <\/strong>and avoid all the external changes to the system times.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-custom-python-timer-classes\"><span class=\"ez-toc-section\" id=\"Custom_Python_Timer_Classes\"><\/span>Custom Python Timer Classes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can create your own custom Timer class to do things according to your need. One main advantage of creating a custom class is you can manage all the time in a single line. You don&#8217;t need to import the time class every time and record the references. Moreover, you can dump your data and record all the past time executions to choose the best algorithm for you.<\/p>\n\n\n\n<p>This section will create a custom class to manage the time and log all the time differences in your codes. Let&#8217;s dive right into it &#8211; <\/p>\n\n\n\n<p><strong>Timer.py &#8211; <\/strong><\/p>\n\n\n\n<p>We&#8217;ll begin with importing the time module. Then initialize the python Timer class and start defining the attributes and methods. So far, we&#8217;ve only included the basic attribute to keep a start reference of the class. To make it more complex, you can create multiple attributes. So far, there are three methods listed below &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>start<\/strong> &#8211; (Optional) To start the timer.<\/li><li><strong>log <\/strong>&#8211; To log the current elapsed time concerning start time.<\/li><li><strong>milestone <\/strong>&#8211; To reset the timer and start its count from 0.<\/li><\/ol>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\nclass Timer:\n\n    def __init__(self):\n        self.start = time.time()\n\n    def start(self):\n        self.start = time.time()\n\n    def log(self):\n        logger = time.time() - self.start\n        print('Time log -',logger)\n\n    def milestone(self):\n        self.start = time.time()\n\n<\/pre><\/div>\n\n\n<p><strong>example.py  &#8211;<\/strong><\/p>\n\n\n\n<p>Examples of usage of the above class are given below. As you can view, you can <strong>keep your code clean<\/strong> by creating a custom Timer class and increase flexibility.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport timer\n\ntime = timer.Timer()\nfor i in range(1000000):\n\tpass\n\tp = 1\ntime.log()\nfor i in range(1000000):\n\tpass\n\tp = 1\ntime.log()\n<\/pre><\/div>\n\n\n<p>Output &#8211; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Time log - 0.054854631423950195\nTime log - 0.10871052742004395<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timer-using-threading\"><span class=\"ez-toc-section\" id=\"Python_Timer_using_Threading\"><\/span>Python Timer using Threading<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>When we want to perform some operation or want our function to run after a certain amount of time, we make use of the <em>Python Timer class. <\/em><strong>The timer class is a subclass of the threading class. <\/strong>Technically, you can say that <em>we create Timer objects when we want actions (functions) bounded by the time.<\/em>&nbsp;<\/p>\n\n\n\n<p>&nbsp;For example, suppose we want to organize a quiz, where the participant has to answer each question in 10 seconds. Here, we can create a timer that will run in the background, and meanwhile, the participant will be thinking about the answer. Now, let us understand how to create a<strong> timer object<\/strong>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Syntax_of_Timer_Class_in_Python\"><\/span>Syntax of Timer Class in Python<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you have not understood what Timer class does, you can think of it in this way \u2013&nbsp;<strong>A Timer Class calls a function after the specified number of seconds<\/strong>.&nbsp;&nbsp;<\/p>\n\n\n\n<p>To start a timer, we need to call the&nbsp;<strong>start()<\/strong>(just like normal threads), and to stop the timer while it is still in the background, we can call&nbsp;<strong>cancel()<\/strong>.&nbsp;<\/p>\n\n\n\n<p>To use the Timer class, we need to&nbsp;<strong><em>import threading<\/em><\/strong>&nbsp;class&nbsp;&nbsp;<\/p>\n\n\n\n<p><strong>threading.Timer(interval, function, args=None, kwargs=None)&nbsp;<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Parameters\"><\/span>Parameters-&nbsp;&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Interval<\/strong>&#8211; The time (in seconds) you want to wait before calling the next function. It can either be in float or integer. For example, for 3 seconds, interval=3.&nbsp;<\/p>\n\n\n\n<p><strong>Function<\/strong>&nbsp;&#8211; The function you want to call after the specified interval of time.&nbsp;<\/p>\n\n\n\n<p>A pervasive way to describe<strong>&nbsp;*args and **kwargs<\/strong>&nbsp;parameters is-<em>&nbsp;create a timer object that runs the function with arguments&#8217; args&#8217; and keyword arguments&#8217; kwargs&#8217; after interval seconds have passed<\/em>. Args should be in the form of a<strong>&nbsp;list<\/strong>&nbsp;and0 keyword args or kwargs in the form of a&nbsp;<strong>dictionary<\/strong>.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Return_Type\"><\/span>Return Type-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It just calls the function specified in the parameters.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Methods_in_Timer_class\"><\/span>Methods in Timer class&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>start()<\/strong>&nbsp;&#8211; It signifies the beginning of execution of the timer.&nbsp;<\/li><li><strong>Cancel ()<\/strong>&#8211; During the timer&#8217;s execution, we can call the cancel if we want to stop it ().&nbsp;<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Creating_a_Timer_Object\"><\/span>Creating a Timer Object&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>a. Understanding the basics<\/strong><\/p>\n\n\n\n<p>To understand the Timer object&#8217;s working, let us create a small program to help us understand the class&#8217;s basics.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the Timer subclass from the threading Class\nfrom threading import Timer\n\n# creating a basic function that will print &quot;hello&quot;\ndef hello():\n    print (&quot;hello, world&quot;)\n\n# creating the object of the Timer subclass\n# Here, 5 sec means that the execution of the function=&quot;hello&quot; after 5 seconds\nt = Timer(interval=5.0, function=hello)\n\n# starting the execution\nt.start() # after 30 seconds, &quot;hello, world&quot; will be printed\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Output-\nhello, world<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"498\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image-1024x498.png\" alt=\"python timer\" class=\"wp-image-3800\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image-1024x498.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image-300x146.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image-768x374.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image-1536x748.png 1536w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/image.png 1586w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You will better understand the functioning of the above program when you try to run it on your own <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/System\" target=\"_blank\" rel=\"noreferrer noopener\">system<\/a>. <\/p>\n\n\n\n<p id=\"h-b-using-the-cancel-method\"><strong>b. Using the cancel method<\/strong><\/p>\n\n\n\n<p>Let us see how to use the <em>cancel() <\/em>function of Timer class.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the Timer subclass from the threading Class\nfrom threading import Timer\n\n# creating a basic function that will print &quot;hello&quot;\ndef hello():\n    print (&quot;hello world&quot;)\n    \n# creating the object of the Timer subclass\n# Here, 5 sec means that the execution of the function=&quot;hello&quot; after 5 seconds\nt = Timer(interval=5.0, function=hello)\n    \n# starting the execution\nt.start() # after 30 seconds, &quot;hello, world&quot; will be printed\n\nprint(&quot;Execution begins&quot;)\n\n# cancelling the execution of the 'hello' function\nt.cancel()\nprint(&quot;END&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Execution begins \nEND<\/pre>\n\n\n\n<p id=\"h-c-how-to-use-the-args-parameter-of-the-python-timer-class\"><strong>c. How to use the &#8216;args&#8217; parameter of the python timer class<\/strong><\/p>\n\n\n\n<p>When we need to give the arguments to the function that needs to be called, we use the args parameter. We must give the args argument in an<em> array.<\/em><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport threading\n\n# To take multiple inputs we can use *before the parameter.\ndef print_name(*names):\n    # From the array of names pick one name and print it\n    for name in names:\n        print(&quot;Hello&quot;,name)\n\n# In the args parameter, give an array of names \nt = threading.Timer(3, print_name,&#x5B;&quot;Ashwini&quot;,&quot;Vandy&quot;,&quot;Arijit&quot;])\n\n# start the execution\nt.start()\nprint(&quot;Execution begins...&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Execution begins...\nHello Ashwini\nHello Vandy\nHello Arijit<\/pre>\n\n\n\n<p>Now that we have talked a lot about time, as a bonus, let us make a program which will act as a Countdown Timer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-countdown-timer-in-python\"><span class=\"ez-toc-section\" id=\"Countdown_Timer_in_python\"><\/span>Countdown Timer in python <span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-time-module\"><span class=\"ez-toc-section\" id=\"Using_the_time_module\"><\/span>Using the time module<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# We will use the time module\nimport time\n\n# Create a function that will print the time\ndef create_countdown_timer(time):\n    print(time,&quot;......&quot;)\n\ntime_in_sec=int(input(&quot;Please entert the time in seconds:&quot;))\n\nfor times in range(time_in_sec):\n    # call the function and pass the current time left\n    create_countdown_timer(time_in_sec-times)\n    # call the function in every 1 second.\n    time.sleep(1)\n    \nprint(&quot;Time is up&quot;)\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Please entert the time in seconds:7\n7 ......\n6 ......\n5 ......\n4 ......\n3 ......\n2 ......\n1 ......\nTime is up<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-using-the-python-timer-class\"><span class=\"ez-toc-section\" id=\"Using_the_python_Timer_Class\"><\/span>Using the python Timer Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# We will use the time module\nimport time\n\nfrom threading import Timer\n\n# Create a function that will print the time\ndef create_countdown_timer(time):\n    print(time,&quot;......&quot;)\n\n# Here you have to enter the time for which the timer will run\ntime_in_sec=int(input(&quot;Please enter the time in seconds:&quot;))\n\n# For the first time we will call the function manually\ncreate_countdown_timer(time_in_sec) \n\nfor times in range(1,time_in_sec): \n    # calling the Timer class every second\n    t = Timer(1,create_countdown_timer,&#x5B;str(time_in_sec-times)])\n    t.start()\n    time.sleep(1)\n    \nprint(&quot;\\n Time is up&quot;)\n    \n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">Please entert the time in seconds:10\n10 ......\n9 ......\n8 ......\n7 ......\n6 ......\n5 ......\n4 ......\n3 ......\n2 ......\n1 ......\nTime is up<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timers-as-context-managers\"><span class=\"ez-toc-section\" id=\"Python_Timers_as_Context_Managers\"><\/span>Python Timers as Context Managers<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Context Managers are the best ways to <strong>avoid memory errors and subsequent crashes<\/strong>. Everyone must&#8217;ve known about &#8220;<em>with<\/em>&#8221; the statement in Python. <strong>This statement ensures that we don&#8217;t have to take care of many objects to close independently. <\/strong>Everyone might have used it with the combination of <em>with <\/em>and<em> open()<\/em> function. <em>So, moving to the main question, can we create Context Managers for Python Timers<\/em>?<\/p>\n\n\n\n<p><strong>Yes. Due to multiple overloading functions, we can easily create our own python timer as a context manager in just a few lines. Let&#8217;s start with an example where you have to measure the program&#8217;s time to run. Without redefining all the variables, one context manager can be used repeatedly to measure the time multiple times. The following program demonstrates it.<\/strong><\/p>\n\n\n\n<p><strong>Example &#8211;<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;import time\n\n\nclass Timer_Pythonpool():\n    &quot;&quot;&quot;\n        &lt;a href=&quot;http:\/\/www.pythonpool.com\/python-mock-context-manager\/&quot; target=&quot;_blank&quot; rel=&quot;noopener&quot;&gt;Context manager as a python&lt;\/a&gt; timer\n    &quot;&quot;&quot;\n    def __init__(self): \n        self.start = None\n          \n    def __enter__(self):\n        &quot;&quot;&quot;\n            Notes the time at the start of the iteration\n        &quot;&quot;&quot;\n        self.start = time.time()\n        return self\n      \n    def __exit__(self, exc_type, exc_value, exc_traceback):\n        &quot;&quot;&quot;\n            Prints the time taken at the end of the iteration\n        &quot;&quot;&quot;\n        print(&quot;Time to finish the task: &quot;, time.time()-self.start)\n  \n  \nwith Timer_Pythonpool() as timer: \n    for i in range(1000000):\n        x = 0\n        pass&lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<p><strong>Output &#8211; <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Time to finish the task:  0.05392050743103027<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>We start by creating a class named  &#8220;<strong>Timer_Pythonpool<\/strong>.&#8221; Then we customize the operators to make them useful as a context manager.<em> <\/em><strong><em>__enter__ <\/em>function executes at the start of the context, and <em>__exit__<\/em> executes at the end of context<\/strong>. Creating a reference point between these two functions can give you the exact time taken by the context to execute.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timer-decorators\"><span class=\"ez-toc-section\" id=\"Python_Timer_Decorators\"><\/span>Python Timer Decorators<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Decorators are the additional support for any kind of function. <strong>Also termed as metaprogramming, you can modify\/add functionality to it. Python Timer Decorators are the easiest way to implement the timer functions in python. <\/strong>Once declared, you can use the decorators in one line without even knowing them. Adding to it, you can apply them to every function in your code to check which code takes the most time to execute.<\/p>\n\n\n\n<p><strong>Syntax &#8211; <\/strong><\/p>\n\n\n\n<p>To create a <strong>chaining decorator, you need to declare multiple nested functions<\/strong>. Name the other function as your main decorator name and inner to any random name. The<strong> inner function will fetch the reference of the function<\/strong> used below the decorator.<\/p>\n\n\n\n<p><strong>Example &#8211; <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\ndef check_time(func):\n    def inner(*args, **kwargs):\n        start = time.time()\n        func(*args, **kwargs)\n        end = time.time()\n        print(&quot;Time taken to execute function is &quot;, end-start)\n    return inner\n\n\n@check_time\ndef task():\n    # do something\n    for i in range(10000000):\n        x = 0\n        pass\n\n\ntask()\n<\/pre><\/div>\n\n\n<p><strong>Output &#8211; <\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Time taken to execute function is  0.24933218955993652<\/code><\/pre>\n\n\n\n<p><strong>Explanation &#8211; <\/strong><\/p>\n\n\n\n<p>As usual, we start with importing the most important module from python &#8220;<strong>time<\/strong>.&#8221;  Nextly, we create a decorator named &#8220;<strong>check_time<\/strong>.&#8221; Inside it, we add a nested inner function where we can create the references of time. <strong>These two references are placed so that both of them are placed between the function execution.<\/strong><\/p>\n\n\n\n<p>Further, we create a testing function named &#8220;task&#8221; to check if our task works. Then add a decorator on top of it. Now<strong> the decorator will do its magic and print the time taken by the function.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-python-timer-cooldown\"><span class=\"ez-toc-section\" id=\"Python_Timer_Cooldown\"><\/span>Python Timer Cooldown<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python Timer Cooldown is a way to measure the timer backward. By creating a custom timer class, we can log the time at every point of your code. You can export this class as a module and install it as a dependency in your code. Then by using a single line, you can import it &#8211;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport time\n\nclass Timer:\n    &quot;&quot;&quot;\n        Timer class\n    &quot;&quot;&quot;\n    def __init__(self):\n        self.start = time.time()\n\n    '''\n    Restarts the timer.\n    '''\n    def restart(self):\n        self.start = time.time()\n\n\n    '''\n    Returns the time elapsed and resets the counter.\n    '''\n    def get_new_time(self):\n        value = time.time() - self.start\n        self.restart()\n        return value\n\n\n    '''\n    Prints the time elapsed and resets the counter.\n    '''\n    def print_new_time(self):\n        print (self.get_new_time())\n\n\n    '''\n    Returns the time elapsed (Does not reset the counter).\n    '''\n    def get_time(self):\n        return time.time() - self.start\n        self.restart()\n\n\n    '''\n    Prints the time elapsed (Does not reset the counter).\n    '''\n    def print_time(self):\n        print(self.get_time())\n\n\n    '''\n    Returns the time elapsed in HH:mm:ss (Does not reset the counter).\n    '''\n    def get_time_hhmmss(self):\n        end = time.time()\n        m, s = divmod(end - self.start, 60)\n        h, m = divmod(m, 60)\n        time_str = &quot;%02d:%02d:%02d&quot; % (h, m, s)\n        return time_str\n\n\ntimer = Timer() #Initialize the timer\n#wash clothes for 5 seconds\ntimer.print_time() #Print the time elapsed since Initialization (in seconds)\n#dry clothes for 3 seconds\ntimer.print_new_time() #Print the time elapsed since Initialization and reset the timer\n#burn clothes for 10 seconds\nprint(str('Task done for ' + str(timer.get_time()) + ' seconds.'))\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-other-python-timer-modules\"><span class=\"ez-toc-section\" id=\"Other_Python_Timer_Modules\"><\/span>Other Python Timer Modules<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Python contains thousands of modules and millions of code snippets. We can always use the open-source modules to use python timers. Github is the biggest place to find such modules. Let&#8217;s jump right into these modules &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/github.com\/trehn\/termdown\" rel=\"nofollow\">termdown<\/a>:  An advanced python timer made by using different ASCII characters. Using this script, you can create a simple countdown timer in your terminal and execute a command at the end. Most importantly, it has support for voice countdown.<\/li><li><a href=\"https:\/\/github.com\/MobProgramming\/MobTimer.Python\" rel=\"nofollow\">MobTimer.Python<\/a>:  A GUI based timer made in python. This script creates full screen-based timers with multiple options. Along with it, you can run multiple timers on your screen at the same time by using this program.<\/li><li><a href=\"https:\/\/github.com\/chriscz\/timer\" rel=\"nofollow\">timer<\/a>: This is the most basic GUI based timer made in python (Tkinter). Features like multiple timers, labels, and multithreading make it more viable over other programs. <\/li><li><a href=\"https:\/\/github.com\/realpython\/codetiming\" rel=\"nofollow\">codetiming<\/a>: This timer class records all your past execution times for a specific named process. You can check the minimum time, maximum time, meantime, and median time for the specific process along with records. This module can be used in multiple ways, namely, as a context manager and as a decorator.<\/li><li><a href=\"https:\/\/github.com\/Chaordix\/cTimer\" rel=\"nofollow\">cTimer<\/a>: cTime is a nanosecond-precision timer in python. This module uses the c language API to record the time precisely. If you&#8217;re looking for a hardcore precision time recording modules, this is the best one. (Note: In recent updates of python, the time module is added with functions to record time in nanoseconds)<\/li><\/ol>\n\n\n\n<h2 class=\"has-vivid-red-color has-text-color wp-block-heading\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span>Must Read:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Best Book to Learn Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p> Mostly, everyone uses timers for three purposes. The first one being, to record the time for your code execution. And second one being, to add a reminder for something by using a timer. Fortunately, both of these can be created in Python. By using different modules and libraries, you can ensure that you record the time with pinpoint accuracy.<\/p>\n\n\n\n<p>Last but not the least, The timer is a subclass of the threading Class in python. If we want to run any function after a certain interval of time, we can use the python timer class. In the args parameter, which is None by default, we can specify the arguments we want to give to the<strong><em>&nbsp;callback method<\/em><\/strong>.<\/p>\n\n\n\n<p>Try to run the programs on your side and let us know if you have any queries.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python has thousands of modules and libraries to satisfy the coder&#8217;s demands. However, to make them useful, you have to understand them first and then &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Understanding the Python Timer Class with Examples\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-timer\/#more-3799\" aria-label=\"More on Understanding the Python Timer Class with Examples\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":12319,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[1904,1909,1911,1908,1889,1896,1906,1903,1893,1892,1901,1902,1890,1895,1907,1891,1905,1897,1898,1899,1910,1887,1888,1900,1894],"class_list":["post-3799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-10-minute-timer-python","tag-countdown-timer-in-python","tag-countdown-timer-python","tag-creating-a-timer-in-python","tag-how-to-make-a-timer-in-python","tag-how-to-set-a-timer-in-python","tag-python-3-how-to-set-a-repetitive-timer","tag-python-3-timer","tag-python-3-timer-example","tag-python-countdown-timer","tag-python-threading-timer","tag-python-threading-timer-example","tag-python-timer","tag-python-timer-class","tag-python-timer-event","tag-python-timer-example","tag-python-timer-function","tag-python-timer-module","tag-threading-timer-python","tag-timer-countdown-print-python","tag-timer-function-python","tag-timer-in-python","tag-timer-python","tag-timer-python-3","tag-tkinter-timer-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Understanding the Python Timer Class with Examples - Python Pool<\/title>\n<meta name=\"description\" content=\"If we want to delay the execution of a function by a certain amount of time, we can use the python timer class. start() and cancel() are two of its methods.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-timer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding the Python Timer Class with Examples\" \/>\n<meta property=\"og:description\" content=\"Python has thousands of modules and libraries to satisfy the coder&#039;s demands. However, to make them useful, you have to understand them first and then\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-timer\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-02T04:49:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-23T05:57:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Python Pool\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Python Pool\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/\"},\"author\":{\"name\":\"Python Pool\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\"},\"headline\":\"Understanding the Python Timer Class with Examples\",\"datePublished\":\"2020-08-02T04:49:31+00:00\",\"dateModified\":\"2021-05-23T05:57:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/\"},\"wordCount\":2614,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png\",\"keywords\":[\"10 minute timer python\",\"countdown timer in python\",\"countdown timer python\",\"creating a timer in python\",\"how to make a timer in python\",\"how to set a timer in python\",\"python 3 how to set a repetitive timer\",\"python 3 timer\",\"python 3 timer example\",\"python countdown timer\",\"python threading timer\",\"python threading timer example\",\"python timer\",\"python timer class\",\"python timer event\",\"python timer example\",\"python timer function\",\"python timer module\",\"threading timer python\",\"timer countdown print python\",\"timer function python\",\"timer in python\",\"timer python\",\"timer python 3\",\"tkinter timer python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-timer\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-timer\/\",\"name\":\"Understanding the Python Timer Class with Examples - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png\",\"datePublished\":\"2020-08-02T04:49:31+00:00\",\"dateModified\":\"2021-05-23T05:57:36+00:00\",\"description\":\"If we want to delay the execution of a function by a certain amount of time, we can use the python timer class. start() and cancel() are two of its methods.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-timer\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png\",\"width\":1350,\"height\":650,\"caption\":\"python timer\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-timer\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding the Python Timer Class with Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998\",\"name\":\"Python Pool\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g\",\"caption\":\"Python Pool\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Understanding the Python Timer Class with Examples - Python Pool","description":"If we want to delay the execution of a function by a certain amount of time, we can use the python timer class. start() and cancel() are two of its methods.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-timer\/","og_locale":"en_US","og_type":"article","og_title":"Understanding the Python Timer Class with Examples","og_description":"Python has thousands of modules and libraries to satisfy the coder's demands. However, to make them useful, you have to understand them first and then","og_url":"https:\/\/www.pythonpool.com\/python-timer\/","og_site_name":"Python Pool","article_published_time":"2020-08-02T04:49:31+00:00","article_modified_time":"2021-05-23T05:57:36+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png","type":"image\/png"}],"author":"Python Pool","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Python Pool","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-timer\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/"},"author":{"name":"Python Pool","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998"},"headline":"Understanding the Python Timer Class with Examples","datePublished":"2020-08-02T04:49:31+00:00","dateModified":"2021-05-23T05:57:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/"},"wordCount":2614,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png","keywords":["10 minute timer python","countdown timer in python","countdown timer python","creating a timer in python","how to make a timer in python","how to set a timer in python","python 3 how to set a repetitive timer","python 3 timer","python 3 timer example","python countdown timer","python threading timer","python threading timer example","python timer","python timer class","python timer event","python timer example","python timer function","python timer module","threading timer python","timer countdown print python","timer function python","timer in python","timer python","timer python 3","tkinter timer python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-timer\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-timer\/","url":"https:\/\/www.pythonpool.com\/python-timer\/","name":"Understanding the Python Timer Class with Examples - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png","datePublished":"2020-08-02T04:49:31+00:00","dateModified":"2021-05-23T05:57:36+00:00","description":"If we want to delay the execution of a function by a certain amount of time, we can use the python timer class. start() and cancel() are two of its methods.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-timer\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-timer\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-timer\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/Understanding-the-Python-Timer-Class-with-Examples.png","width":1350,"height":650,"caption":"python timer"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-timer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Understanding the Python Timer Class with Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/f87448ee54c0ffd2889fbf2408c18998","name":"Python Pool","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdd3cb9ad7f560324dfd481989550aa8ffce84388fd253c42beca35c999d3108?s=96&d=wavatar&r=g","caption":"Python Pool"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3799"}],"version-history":[{"count":8,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3799\/revisions"}],"predecessor-version":[{"id":24373,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3799\/revisions\/24373"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/12319"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}