Python sleep() command with example. Add Delay In A Seconds To Suspend Execution Of A Script.

Python sleep() command with example. Add Delay In A Seconds To Suspend Execution Of A Script.

Python is fully functional and widely used programming language. This language is similar to C, and Java. There are few differences between all languages. Python consists of many functions and all works well. Here now we are talking about the sleep() which is mostly used by the developer while programming.
This method used to suspend the execution for given of time(in seconds). Here the parameter can be a floating point number to for more precise sleep time.

Python sleep():
With the number of methods, this language has become more powerful and developers are very happy to use it. Here we are going to know about the Python sleep() and how does it work. If you are using this method, you need to use a module which is called time. This time, the module provides a number of time-related functions for developers.

Sleep Syntax In Python
The syntax for sleep() is as follows
import time
time.sleep(1)
time.sleep(2)
time.sleep(3)
time.sleep(N)
Here time is a module of this programming language and sleep is a function and here it is called using the dot(.) operator. in the brace, there is "N" which indicate the number of execution. Using one of the sleep methods, execution can be suspended for the given number of time.

See The Example:
#!/usr/bin/python
import time
print "Start : %s" % time.ctime()
time.sleep( 5 )
print "End : %s" % time.ctime()
If you run given code so you will get the following result:
Start : Wed Oct 05 11:18:19 2017
End : Wed Oct 05 11:23:26 2017
How to delay execution for few seconds :
As we have seen and know well that sleep function is used to delay the execution but now we are trying to know that what can the time for execution as an example.

You always have to import "time" is you want to use sleep()
time.sleep(1) # delays for 1 seconds
time.sleep(10) # delays for 10 seconds
time.sleep(60) # delays for 1 minute
time.sleep(3600) # delays for 1 hour

Here all the values given in braces() is in second but if calculate so here we also mentioned delay for an hour in second like 3600, so if you want to delay for an hour so you must provide the value in seconds.

Script To Add Delay In A Seconds To Suspend Execution :
Here we are creating a very simple script for delaying a second for an execution by giving time in seconds with full example so get ready to make a script and see the result.
import time
def main():
while True:
print("DateTime " + time.strftime("%c"))
time.sleep(1) # delays for 1 seconds
if __name__ == '__main__':
main()
The final output will be:
DateTime 07/25/16 18:03:42
DateTime 07/25/16 18:03:43
DateTime 07/25/16 18:03:44
DateTime 07/25/16 18:03:45
DateTime 07/25/16 18:03:46
DateTime 07/25/16 18:03:47
Hope this will assist you and you have learned sleep method in python for delaying the process as given time interval so just try and become an expert.
Author
bhawanisingh
Views
3,358
First release
Last update
Rating
0.00 star(s) 0 ratings
Top