In this tutorial, we will discuss the python time.sleep() method and also discuss its uses.
Python time.sleep()
sleep() is a method of time module and it is one of the most interesting methods of time module. It can delay the execution of statement for related-time.
Let’s understand it with an example
import datetime
import time
print('1st statement printed at', datetime.datetime.today().strftime("%H hour:%M minute:%S second"))
delay_seconds = 10
time.sleep(delay_seconds)
print('2nd statement printed at', datetime.datetime.today().strftime("%H hour:%M minute:%S second"))
Output
1st statement printed at 21 hour:41 minute:49 second
2nd statement printed at 21 hour:41 minute:59 second
Behind the code
In the above example, we printed two statements and between them, we have used the time.sleep() method. The second statement executes and printed just after 10 seconds the first, this is because in the time.sleep() method we passed delay_variable with value 10 and it delays the next execution by 10 seconds.
Digital clock in Python
With the help of time.sleep() statement we can create a digital watch. The example we have provided here contains an infinite loop so please use the ctrl + fn + C button to break out through the program.
Digital watch in python
import time
import datetime
while True:
print("*****************")
print("**",datetime.datetime.today().strftime("%H:%M:%S %p"),"**")
print("*****************\n\n")
time.sleep(1)
Output
*****************
** 22:34:32 PM **
*****************
*****************
** 22:34:33 PM **
*****************
*****************
** 22:34:34 PM **
*****************
*****************
** 22:34:35 PM **
*****************
*****************
** 22:34:36 PM **
*****************
*****************
** 22:34:37 PM **
*****************
*****************
** 22:34:38 PM **
*****************
Multithreading in Python
Multithreading means performing managing and performing different tasks at once, python has a very useful module known as threading which allows us to execute more than two functions at once before one gets executed completely. Normally when we call two functions the python executes that function first which is called first but in threading the function execution does not depend on function calling, it all about the processing speed and time of the CPU. The main purpose of multithreading is it reduces the time taken to execute the complete program.
Normal function calling
def func_1():
for i in range(3):
print("Func_1")
def func_2():
for i in range(3):
print("Func_2")
func_1()
func_2()
Output
Func_1
Func_1
Func_1
Func_2
Func_2
Func_2
Behind the code
In the above example use the single threading and the function execute according to the position of function calling. The func_1() function execute first because it called first and the func_2() execute after the func_1() get completely executed.
Multithreading function execution
import threading
def func_1():
for i in range(5):
print("##Func_1##")
def func_2():
for i in range(5):
print("**Func_2**")
t1 = threading.Thread(target=func_1)
t2 = threading.Thread(target=func_2)
t1.start()
t2.start()
Output
##Func_1##
##Func_1##
**Func_2**
**Func_2**
##Func_1##
**Func_2**
**Func_2**
##Func_1##
**Func_2**
##Func_1##
Behind the code
The above example is similar to the previous one but the only difference is here the func_2 did not wait for the complete execution of func_1. The threading module is capable of executing two or more than 2 functions simultaneously.
time.sleep() with a multithreading program
As we know that the time.sleep() method can delay the execution of the next statement by some seconds. With the help of the time.sleep() method we can more easily observe the working of multithreading programs.
Example
import threading
import time
def func_1():
for i in range(5):
time.sleep(2)
print("##Function 1 has called##")
def func_2():
for i in range(5):
time.sleep(2)
print("**Function 2 has called**")
t1 = threading.Thread(target=func_1)
t2 = threading.Thread(target=func_2)
t1.start()
t2.start()
Output:
##Function 1 has called##
**Function 2 has called**
##Function 1 has called##
**Function 2 has called**
##Function 1 has called##
**Function 2 has called**
##Function 1 has called##
**Function 2 has called**
##Function 1 has called##
**Function 2 has called**
Leave a Comment on this Post