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.

Syntax

Import time

Time.sleep(sec)

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.

Import time

Print(“Hello world”)

Time.sleep(2)

Print(“sleep python function”)

Time.sleep(5)

Print(“sleep function is working”)

We will run this code on the spyder tool and will see the output after execution is completed.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image2-50.png" data-lazy- height="360" src="data:image/svg xml,” width=”720″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image4-49.png" data-lazy- height="409" src="data:image/svg xml,” width=”716″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image3-51.png" data-lazy- height="373" src="data:image/svg xml,” width=”715″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image6-40.png" data-lazy- height="394" src="data:image/svg xml,” width=”719″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image5-44.png" data-lazy- height="407" src="data:image/svg xml,” width=”477″>

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.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/image1-50.png" height="199" src="data:image/svg xml,” width=”267″>

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.

Conclusion

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.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/Author-Image-150×150.jpg60d31bd477440.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.