{"id":6968,"date":"2020-11-20T17:12:04","date_gmt":"2020-11-20T11:42:04","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=6968"},"modified":"2020-11-30T13:26:46","modified_gmt":"2020-11-30T07:56:46","slug":"python-time-sleep-examples-to-add-delay-to-your-code","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-time-sleep-examples-to-add-delay-to-your-code\/","title":{"rendered":"Python time sleep: Examples to add DELAY to your code"},"content":{"rendered":"\n<p>Sometimes you need to add delay to your specific code. <a href=\"https:\/\/en.wikipedia.org\/wiki\/Python_(programming_language)\" rel=\"nofollow noopener\" target=\"_blank\">Python <\/a>has a built-in Sleep Function for such functionality. The sleep() function is part of the time module.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using sleep() function in Python<\/h2>\n\n\n\n<p>Let\u2019s take a look at how to use the sleep functions in Python. We will use the current time to check the time difference.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>import time<br>t = time.localtime()<br>current_time = time.strftime(\"%H:%M:%S\", t)<br>print(current_time)<br>print(\"I am here\")<br>time.sleep(5)<br>print(\"I had to wait 5 seconds\")<br>t = time.localtime()<br>current_time = time.strftime(\"%H:%M:%S\", t)<br>print(current_time)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The following is the output.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/HHKLu-K0Q2mASx5LOvYLga0hHwnWsvM_OKRsQ1vgAjLYPUjd2Hxrkt-ShteNDu4JjuUS68RG0iM6EiQrbUyw6qPSsaCb_gVQKrUopcvwbnalScMiGRTTERRB-L-OyF8Gh6V_TmZsPWPurBB3Dw\" alt=\"Python sleep function\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Delay function execution<\/h2>\n\n\n\n<p>Use the time.sleep function in the user-defined function to delay the execution of the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-functions-examples\/\">python function<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>import time<br>print('Code Execution Started')<br>def display():<br>time.sleep(5)<br>print('I am in function')<br>&nbsp; &nbsp;<br>print('Function Execution Delayed')<br>display()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/cD2pje6bkFOzxs2z9Q8n4L0rjVT0A_AG2fSGEobdoXqV862fxlZaT7LK57zec5nP6leA8RaRxBfu5wDYK8K7DXQ2UIbtlCXRLPEeWeS3Xg9TT4cHWuuctU0nizpbvR_Fv5Rw5OnCiPmKSfud1Q\" width=\"612\" height=\"536\" alt=\"\" title=\"\">]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding delay using <strong>Event().wait <\/strong>function<\/h2>\n\n\n\n<p>In the threading library there is a function called event.wait() that can be used to add delay in the code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>from threading import Event<br><br>print('Code Execution Started')<br>def display():<br>print('I am in the function')<br>Event().wait(5)<br>display()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/cC5WfA3dUT1pDicinKxAPH_5dNGlIMwOXUliCV0VoADZ4OQZWSXtj-ANsiQrIGOqPZnvIDy_K8Xvm-Yu0V6Ci4v2XXkVXCW3QR7s9k2cOuuP3ISMjmeRpk3EUyHeTzSCaK2HQlKWDf21mr5MEg\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using Timer for delay<\/h2>\n\n\n\n<p>We can also use timer function to add delay to code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>from threading import Timer<br>print('Code Execution Started')<br>def display():<br>print('I am in the function')<br>t = Timer(5, display)<br>t.start()<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/aiCJ4xtJloPnaDtKBdh5mrlx14bHn3fqo-jfR-Hbto9ymZPdOrlia3AN7M9TrVk63P3rp-peE_A9e0Z25VQJvP29UMZfQBWMhi5yPEdZiY84-CC8kaKygLeaaHwtem-Zbe7hFBUCIKxUO40uHA\" alt=\"Python sleep function\" title=\"\"><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need to add delay to your specific code. Python has a built-in Sleep Function for such functionality. The sleep() function is part of the time module.&nbsp; Using sleep() function in Python Let\u2019s take a look at how to use the sleep functions in Python. We will use the current time to check the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6973,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-6968","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/6968","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=6968"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/6968\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/6973"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=6968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=6968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=6968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}