Python time.sleep(): Add Delay to Your Code (Example)

What is Python Sleep?

Python sleep() is a function used to delay the execution of code for the number of seconds given as input to sleep(). The sleep() command is a part of the time module. You can use the sleep() function to temporarily halt the execution of your code. For example, you are waiting for a process to complete or a file upload.

time.sleep() Syntax

import time
time.sleep(seconds)

Parameters

seconds: The number of seconds you want the execution of your code to be halted.

Example: Using sleep() function in Python

Follow the steps given below to add sleep() in your python script.

Step 1:

import time

Step 2: Add time.sleep()

The number 5 given as input to sleep(), is the number of seconds you want the code execution to halt when it is executed.

time.sleep(5)

Here is a working code along with messages inside print(), to show the delay of message display on the terminal when executed.

import time
print("Welcome to guru99 Python Tutorials")
time.sleep(5)
print("This message will be printed after a wait of 5 seconds")

Output:

Welcome to guru99 Python Tutorials
This message will be printed after a wait of 5 seconds

How to delay the execution of function using sleep()?

The example shown below has a function defined called display(). The display() function prints a message “Welcome to Guru99 Tutorials”. When the function is called, it will execute and display the message inside the terminal.

To add delay to the execution of the function, let us add the time.sleep in Python before making a call to the function. During the execution, Python time.sleep will halt there for the number of seconds given, and later the function display() will be called.

Example:

import time

print('Code Execution Started')

def display():
    print('Welcome to Guru99 Tutorials')
    time.sleep(5)

display()
print('Function Execution Delayed')

Output:

Code Execution Started
Welcome to Guru99 Tutorials
Function Execution Delayed

What are the different ways to add a delay in Python Script?

Using sleep() function

We have seen a few examples earlier on how to use time.sleep(). Let us try a different example here using time.sleep().

Example:

The code has a for loop that will take the string variable and print each character with a delay of 1 seconds.

import time
my_message = "Guru99"
for i in my_message:
   print(i)
   time.sleep(1)

Output:

G
u
r
u
9
9

Using asyncio.sleep function available from (Python 3.4 or higher)

You can make use of asyncio.sleep with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function, as shown in the example below:

Example:

The script has a function call display() that prints a message “Welcome to Guru99 tutorials”. There are two keywords used in the function async and await. The async keyword is added at the start of the function definition, and await is added just before the asyncio.sleep(). Both the keywords async / await are meant to handle the asynchronous task.

When the function display() is called, and it encounters await asyncio.sleep(5), the code will sleep or halt at that point for 5 seconds and, once done, will print the message.

import asyncio

print('Code Execution Started')

async def display():
    await asyncio.sleep(5)
    print('Welcome to Guru99 Tutorials')

asyncio.run(display())

Output:

Code Execution Started
Welcome to Guru99 Tutorials

Using Event().wait

The Event().wait method comes from the threading module. Event.wait() method will halt the execution of any process for the number of seconds it takes as an argument. The working of Event is shown in the example below:

Example:

The code is using Event().wait(5).The number 5 is the number of seconds the code will delay to go to the next line that calls the function display(). Once the 5 seconds are done, the function display() will be called, and the message will be printed inside in the terminal.

from threading import Event

print('Code Execution Started')

def display():
    print('Welcome to Guru99 Tutorials')


Event().wait(5) 
display()

Output:

Code Execution Started
Welcome to Guru99 Tutorials

Using Timer

The Timer is another method available with Threading, and it helps to get the same functionality as Python time sleep. The working of the Timer is shown in the example below:

Example:

A Timer takes in input as the delay time in Python in seconds, along with a task that needs to be started. To make a timer working, you need to call the start() method. In the code, the Timer is given 5 seconds, and the function display that has to be called when 5 seconds are done. The timer will start working when the Timer.start() method is called.

from threading import Timer

print('Code Execution Started')

def display():
    print('Welcome to Guru99 Tutorials')

t = Timer(5, display)  
t.start()

Output:

Code Execution Started
Welcome to Guru99 Tutorials

Summary

  • Python sleep() function will pause Python code or delay the execution of program for the number of seconds given as input to sleep(). The sleep() function is part of the Python time module.
  • You can make use of Python sleep function when you want to temporarily halt the execution of your code. For example, in case you are waiting for another process to complete, or a file upload, etc.
  • There are many ways to add Python delay function to code besides sleep, and they are using asyncio.sleep , Event().wait and Timer.
  • Similar to sleep() method, there is asyncio.sleep() method with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function
  • The Event().wait method comes from the threading module. Event.wait() method will halt the execution of any process for the number of seconds it takes as an argument.
  • The Timer is another method available with Threading, and it helps to get the same functionality as sleep