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 the sleep() function. The sleep() function of the time module is used to suspend or halt the current code or thread’s execution 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 in a floating-point number, which can be used to specify the accurate sleep time to the argument. Let us syntax and examples of this function in the below sections.
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:
print("Simple program to demonstrate sleep function:")
print("The Start time is as follows : ")
time.sleep( 5 )
print("The End time is as follows:" )
Explanation: In the above program, we can see that firstly we need to import the time module to used the 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 the sleep() function to stop the execution and then display the current time when the function is stopped executing the thread using the sleep() function.
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.
A dramatic way of printing the given message or String.
print("Program to demonstrate sleep() function used for printing words letter by letter:")
strn = "Educba"
print("The given word is as follows:")
for i in range(0, len(strn)):
print(strn[i], end ="")
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 the sleep() function. In this, we have the time specified as 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 the sleep() function, which is important for multithreading as the sleep() function is used to stop the current thread’s execution, which is done only during the multithreading process. Let us see how to demonstrate this in Python using the time method sleep().
from threading import Thread
for x in range(0, 12):
for x in range(100, 103):
print("Starting Worker Thread will start from :")
print("Starting Waiter Thread will stop at:")
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 range from 0 to 12, there will be 3 threads created in each with 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.
In this article, we conclude that we have seen the syntax and working of Python’s sleep() function 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 the 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 sleep() function applications, which are used in dramatic printing and in multi-threading.
This is a guide to Python Sleep. Here we discuss a brief overview of the Python Sleep method and its Examples, along with its Code Implementation. You can also go through our other suggested articles to learn more –