Python time sleep | Delay in execution of a program

Today we will discuss the sleep() function present in Python. Sleep() function in Python is used to bring a delay in the execution of a program for a required time. The sleep function is a part of the time module and hence to access this function, we need to import the time module. It takes the number of seconds as an argument to the function and halts the execution for the given number of seconds.

The important point about the sleep function is that it stops the execution of only the current thread rather than the whole program. A simple example using this function is:

import time
print ("delay created")


delay created

In the above code, we import the time module to access the sleep function. Here, we have passed 2 seconds as the argument to the sleep function. Hence when the code is run in the python console, it is observed that the print statement is executed after a delay of 2 seconds.

With the help of sleep function, we can even have different delay times in a single program like,

import time 
l=['Good', 'Morning', 'Everyone']
for i in [0, 1, 2]:
    print("delay of ",i, "seconds")


delay of 0 seconds
delay of 1 seconds
delay of 2 seconds

Also read: Partial Functions in Python

Sleep function for a multithreaded program in Python

A thread is the smallest unit within an OS and is a part of the process( Process refers to the execution of the collection of instructions). When more than one thread is present inside a process, the program is referred to as a multithreaded program. In a multithreaded program, the sleep function suspends the execution of only the current thread, unlike the single-threaded programs where it suspends the thread along with the process. Here is an example of a multithreaded program with sleep function.

import time
from threading import Thread

class batch(Thread):
    def run(self):
        for i in range(0, 10):

class number(Thread):
    def run(self):
        for j in range(50, 53):

print("Start the First Thread: batch")
print("Start the Second Thread: number")


Start the First Thread: batch
Start the Second Thread: number

In the above code whenever the sleep function is called, it suspends the execution of the current thread and not the entire program.

How does sleep() functionality work?

The sleep function uses a system clock that emits high-frequency signals. These signals synchronize all the internal components of the computer. This function generates an interrupt request that interrupts the execution of the current code. It saves the current state of the code and then calls the asynchronous procedure call that blocks the execution. The OS stops the process for a particular period. Once this time is elapsed the execution of the process resumes from the very last state.


The sleep function can be used for several reasons like,

  • For dramatic printing, the example for this is shown below:
    import time
    string = " SCARY" 
    for i in string: 

    This code brings about suspense and creates a dramatic effect by printing each character one by one. Note that the visual effect for the above code can be seen only in the editor window.

  • To design some basic and simple tools like the digital clock
  • To halt the execution of a program during uploading or downloading a file.
  • To wait for a graphic to get loaded and drawn on the screen.
  • To check the status of a website or a user interface during an automated test.
  • To add delays in between downloading millions of files within a GUI in order to avoid a server from slowing down.


Leave a Reply

Your email address will not be published. Required fields are marked *