Blog / Python sleep() function

Python sleep() function

by SW Team

Python time sleep

The Python time sleep function is used to slow down the execution of a program. We can use the Python sleep function to pause the execution of the program for a specific amount of time in seconds. It is important to note that the Python sleep function only stops the execution of the current thread and not the entire program.

Syntax of the Python sleep() function

The Python sleep() method belongs to the Python time module. In order to use this method, it is necessary to import this module first. The Python sleep() function is used as follows:

 # We import the time module
 import time
 time= 4 # 4 seconds
 time.sleep(time)

The argument to the sleep() method is in seconds. Therefore, when executing the time.sleep(time) statement, the following line of code will be executed after "time" (4) seconds.

An example is shown below:

# Import the time module
import time
print("Before declaring the function time.sleep")
time.sleep(4)
print("After declaring the function time.sleep")

If you run the previous code, you will notice that the second printout is made after 4 seconds. Therefore, you can add a delay in your code as needed. In addition, the argument can be a decimal value to get a more precise delay. For example, if you want a delay of 100 milliseconds, i.e. 0.1 seconds, you can do this as follows:

import time
time.sleep(0.100)

cta:cloud_so

Python sleep example

Next, let's look at this example of the Python time sleep function:

import time
start = time.time()
for i in range(10):
    print(i)
    #Pausing for 2 seconds
    time.sleep(2)
fin = time.time()
elapsed_time = end - start
print(f"Elapsed time = {elapsed_time:.2f} segundos")

Exit:

0
1
2
3
4
5
6
7
8
9
Elapsed time = 20.02 seconds

The elapsed time is more than 20 because each time the for loop is executed, execution stops for 2 seconds. The extra time is due to program execution time, operating system thread scheduling, etc.

Setting different delay times with sleep()

Occasionally, it may be desirable to postpone execution at different time intervals. This can be done in the following way:

import time
for i in [0.5, 0.1, 1, 2]:
   print(f"Waiting {i} seconds")
time.sleep(i)

Exit:

Waiting 0.5 seconds
Waiting 0.1 seconds
Waiting 1 seconds
Waiting 2 seconds

cta:cloud_app_swpanel_smart_d5

Python thread sleep

The Python sleep() function is essential for multithreaded programming. Here is a simple example demonstrating how the Python sleep function stops the execution of the current thread in a multithreaded programming environment.

import time
from threading import Thread

class Worker(Thread):
def run(self):
    for x in range(0, 11):
        print(x)
        time.sleep(1)    

class Waiter(Thread):
def run(self):
    for x in range(100, 103):
        print(x)
        time.sleep(5)

print("Starting the Worker's thread")
Worker().start()
print("Starting the Waiter thread")
Waiter().start()
print("Fact")

Exit:

Launching the Worker's thread
0
Starting Waiter thread
100
Done
1
2
3
4
5
101
6
7
8
9
10
102

In the output of the execution you can clearly see that only the threads are being stopped in execution and not the whole program due to Python's sleep function.

cta:hosting

i