Python sleep () is used to delay the specific processes by adding a sleep function in the source code. The time provided in seconds allows a delay in the process. In this article, we will perform many examples to enhance your knowledge of the sleep method in Python. We have used the Spyder tool for the execution of code.
Firstly, to run the code, we need to add the time module. Sleep function is a part of this module, so we will use the time module to use this function access. Only a single parameter shows the number of seconds we have mentioned in the code to pause the python program.
A simple example of sleep function
To understand the concept of sleep function, we have taken a simple example. Three statements are printed with the gap of a specific time. Time delay is done through the sleep function. Firstly, we printed the first line then used the sleep function to delay the output for 2 seconds. Similarly, for the next print, we have used the parameter of 5 seconds. The code is as follows.
Print(“sleep python function”)
Print(“sleep function is working”)
We will run this code on the spyder tool and will see the output after execution is completed.
The output is shown on the right section of the tool in the “console bar.” From the image, it is not clear, but as you run the code and the output is displayed by taking the proper time interval which we have defined in the code.
Sleep function with time function ()
This is an example of how sleep function helps in providing a gap between the periods. We have used the time () function to get the current time and the local time () function to get the specific place’s current time and date. Furthermore, the strftime () function is basically ‘string from time’ that gets the time in the form of the string in the given format. The first step is again importing the time module.
The code is written in the text editor part of the tool. Now run and execute the program; you can check the output on the right side. The result shows that the duration between the two time periods is two seconds.
Creating a digital clock with sleep () and timestamp
In this script, we have created 7 timestamps and added a delay of 2 seconds between each timestamp. The first step is the same as discussed in previous examples. This example is different from the previous one because we have created timestamps more than once using FOR loop. Whereas in the last example, we have obtained the output manually. For loop will iterate 7 times successfully. Again we have used the string for time function to get the time in that specified format. Finally, then sleep function is introduced that will cause a delay of 2 seconds between each timestamp. When the program is executed, we wait for 2 seconds and wait for two seconds for the next timestamp.
Below is the output image; you can observe that the output contains 7 timestamps, each having a delay of 2 seconds.
Sleep () on a string
Applying sleep function on a string is very easy. If you want to display each character of a string having some delay between them, you can use the sleep function. The same task is shown in the script described below. Here we have taken a text value as a string. By applying FOR loop, we have initiated iteration to each character with a delay of 4 seconds. You can see the output that each character is displayed in a separate line because of adding a delay in displaying every iteration of FOR loop.
Sleep function () in multithreading in Python
An operating system consists of many small units known as threads. These threads are responsible for the execution or running of the programs taking place in the processor. When there are multiple threads in any process, the program is known as multithreaded. Sleep function is very useful in multithreading programs as it causes one program to delay for a while when another program is in the state of execution. In multithreading processes using the sleep function, each process is executed on a priority basis. And the other one has to wait unless the first task has completed its execution in the given time. Here also the same concept is discussed. Let’s see the working of sleep () in multithreading.
Here we have imported two modules. One is the time module, and another one is threading as we are working on the threads. After importing modules, we have used two customs functions in the code. First, one defines the first thread function, and the other is to defines the second function. Sleep time is of 1 second in both of the functions. After defining the function, it’s time to set them by assigning the functions connection. After setting each thread, a function is started. The following output is obtained after execution.
The first thread is started; after starting the first thread, the second thread begins, and it waits till the completion of 1 second. In this time, the first thread will complete its 2 iterations. After that second thread will begin and completes its 2 iterations.
In this article, we have grasped the sleep function in different aspects. It is supportive in solving different problems that are time-related. You are now capable of adding delays in your processes to prevent misuse of system resources.