Introduction to Python Sleep Function
In this article, we will see one of the most used functions that are provided by the time module in Python is sleep() function. The sleep() function of the time module is used to suspend or halt the execution of the current code or thread for the given number of times in seconds. The sleep() function which takes an argument as time in the number of seconds which can be specified in floating-point for accuracy. This sleep() function is used in applications like dramatic string printing and this is also used in the multithreading process.
Working of sleep() Function in Python
In this section, we will see the Python time method sleep(). The sleep() function suspends the code or thread execution for the specified number of times in seconds which is in floating-point number which can be used for specifying the accurate sleep time to the argument. Let us syntax and examples of this function in the below sections.
Syntax:
time.sleep(ts)
Parameters of sleep() Function in Python
ts: This parameter is used to specify the time that execution should be suspended in seconds.
This sleep() function will return nothing as it is just used to suspend the function or code or thread for the specified time in seconds.
Examples to Implement sleep() Function in Python
Below are the examples of sleep() Function in Python:
Example #1
Code:
import time
print("Simple program to demonstrate sleep function:")
print("The Start time is as follows : ")
print(time.ctime())
time.sleep( 5 )
print("The End time is as follows:" )
print(time.ctime())
Output:
Explanation: In the above program, we can see that firstly we need to import the time module to used sleep() function as it is the function of a time module in Python. We can see the first start time is displayed using the ctime() function of the time module which is displays the current time. Then we saw the end time to display using sleep() function to stop the execution and then display the current time when the function is stopped executing the thread using sleep() function.
4.8 (8,365 ratings)
View Course
Let us see the uses of the sleep() function or applications of sleep() in the below section. Firstly we will see sleep() function can be used to print the words with one letter at a time for a better user interface. Let us demonstrate this in the below example.
Example #2
A dramatic way of printing the given message or String.
Code:
import time
print("Program to demonstrate sleep() function used for printing words letter by letter:")
strn = "Educba"
print("The given word is as follows:")
print(strn)
for i in range(0, len(strn)):
print(strn[i], end ="")
time.sleep(0.3)
Output:
Explanation: In the above program, we saw that we have a string defined in this program as “Educba” so we are trying to print the word letter by letter by using sleep() function. In this we have the time specified is 0.3 seconds. This means after every 0.3 seconds each letter is printed using for loop having the start range as 0 index of the length of the given string up to the end of the length of the given string and sleep function. To see the visual effect we have to execute the above program in the local Python editor.
Now let us see another use of sleep() function which is important for multithreading. As sleep() function is used to stop the execution of the current thread which is done only during the multithreading process. Let us see how to demonstrate this in Python using time method sleep().
Example #3
Code:
import time
from threading import Thread
class Worker(Thread):
def run(self):
for x in range(0, 12):
print(x)
time.sleep(1)
class Waiter(Thread):
def run(self):
for x in range(100, 103):
print(x)
time.sleep(5)
print("Starting Worker Thread will start from :")
Worker().start()
print("Starting Waiter Thread will stop at:")
Waiter().start()
print("Done")
Output:
Explanation: In the above program, we have imported modules time and thread module from “threading”. In this code, we can see we have created two classes one for starting the worker thread and another class for stopping the execution of the working thread in which the sleep() function is used to specify the time to stop the execution of the thread. In the class worker, we have defined for loop which will print the numbers ranging from 0 to 12 for every one second one by one the number is printed and in the waiter class we have specified as “5” in sleep() function as to after every 5 numbers it should print the next five numbers in next thread and the thread range is specified from 100 to 103. Therefore as the numbers are ranging from 0 to 12 then there will be 3 threads created in each having 5 numbers except the third thread having only two numbers. This process is seen after executing the above program and the screenshot is as seen above.
Conclusion
In this article we conclude that we have seen the syntax and working of the sleep() function in Python with examples. In this article, we saw from which module this sleep() function can be defined. Therefore we saw we need to import a time module that provides various methods and sleep() function is the most popular method of time module in Python. In this article, we saw the simple example of demonstrating sleep() function with the number of seconds specified as an argument. Lastly, we saw the applications of the sleep() function which are used in dramatic printing and in multi-threading.
Recommended Articles
This is a guide to Python Sleep. Here we discuss a brief overview on the Python Sleep method and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –