In many programming applications, there would need to halt the execution of the program or some sections of the program for some small duration. Python makes our life easy to achieve this task easily with the help of a function called time.sleep(). This blog would thoroughly go through the functionality and applications of this function.
Before starting to look into the functionalities of this function, let us look at what all concepts we will concentrate on in this blog,
- What is the use of python time.sleep()
- time module
- Examples of time.sleep() function
- Thread sleep with python
- Dynamic sleep
What is the use of python time.sleep()?
time.sleep() function is used to halt the program flow and let the other technical counterparts execute at the same time, this function is defined in both python versions of 2 and 3 and belongs to a module called ‘time’. This function will add delay to the execution, which will only halt the execution of the current thread but not the whole program.
sleep() function is part of a time module in python, before using this function we need to import the time module using the command below.
Once the module gets imported we can make full utilization of sleep() function. time.sleep() function basically takes only one parameter ‘seconds-Number of seconds’ as a parameter, which gives a delay to execution for those specific number of seconds.
this function will not return anything(void). Let us look into different examples of sleep function to understand it’s working.
Examples of time.sleep() function:
Here in the above example, we have provided 2 as an argument to time.sleep() method and we are introducing that delay after the first print statement so the second statement would be executed after 2 seconds after the execution of the first one.
Note: we can also pass floating-point numbers as an argument to sleep function, suppose if we pass 0.15 as an argument it means to introduce a delay of 150 seconds.
Let us look at another example where we can show the working of this function much clearer on output.
Time at start of execution of this program is: Tue Jan 14 11:46:24 2020 Time at end of execution of this program is: Tue Jan 14 11:46:28 2020
In the above program, we printed the current time at the start and end of the execution of the program with a delay of 4 seconds in the middle. In the output, we can clearly observe that there is a time gap of 4 seconds between the first and second statements.
let us also look at an example where we use sleep() function inside a loop.
1011121314Elapsed Time = 10.002043962478638
In this program above we are introducing a delay of 2 seconds after every print statement that is in for loop, and in output, we see that execution time is elapsed for 10 seconds because there were 5 print statements.
In python, we can introduce different delay times based on our requirements. In this example let us look at how can we introduce different delays with sleep function.
import time for i in [0.1, 1, 2, 0.35]: print("I will delay this for %s" % i, end='') print(" seconds") time.sleep(i)
I will delay this for 0.1 seconds I will delay this for 1 seconds I will delay this for 2 seconds I will delay this for 0.35 seconds
While the execution of this program you would observe that each print statement would be delayed according to the specific seconds mentioned.
In this example, we will look at something fancy called “lazy printing” where we will be printing each letter in a string after a specific delay using sleep() function.
If you execute this code above you will see every letter will be printed after 0.2 seconds of delay which will look fancy.
Thread sleep with python:
sleep() method can be effectively used while we want to introduce delays in the execution of threads in
a multithreaded environment.
Staring Runner Thread 1 Starting Delay Thread 205Done 2 3 4 5 206 6 7 8 207 208
In the above example, we have started two threads parallelly and also introduced delays of 1 and 5 seconds respectively while executing both threads, so in output, we clearly can see that after the execution of first thread 5 times the second thread is printing its output because we have delay 5 times higher in the second one.
Dynamic sleep with python:
We can also insert the delays dynamically using the sleep() function in python, let us look at the below example which accepts delay time from the user and halts the execution accordingly.
Enter delay time: 3 time Before exec: Tue Jan 14 13:36:37 2020 time After exec: Tue Jan 14 13:36:40 2020n Enter delay time: 4 time Before exec: Tue Jan 14 13:36:54 2020t Time After exec: Tue Jan 14 13:36:58 2020n Enter delay time: rNumber only.n
In this blog, we have seen that how to actually introduce delays using sleep() function in Time module of python in execution of programs, we have seen a lot of coding examples and as well lazy printing which is very fancy along with working with delays in multi-threaded environments and also introducing dynamic delays by taking user inputs.