Python is one of the most popular programming languages today, and mastering it brings an array of advantages to your career. In this article, you will learn everything about sleep in python. Let us start with understanding what exactly is sleep().

What Is Sleep() in Python?

The sleep() function in python’s time module is used to suspend the execution of a program for the given number of seconds. This means that the program is paused for the given time and after that time has passed, the program gets executed automatically.

Here, is a simple step by step description of how the execution of a program works when sleep() is called.

sleepinPython

Syntax of Sleep()

time.sleep (secs)

Parameter

The time.sleep() function has only parameter, i.e secs. This denotes the number of seconds for which we want the program to be suspended. The number of seconds could either be in int or float.

Become a Certified Expert in AWS, Azure and GCP

Caltech Cloud Computing BootcampExplore Program
Become a Certified Expert in AWS, Azure and GCP

How to Add a Python Sleep() Call with time.sleep()

Step1:

To import the time module present in python.

SleepinPython_1

Step 2:

Adding time.sleep()

SleepinPython_2.

Argument 10 passes here is the number of seconds for which the execution of the program will be halted.

Example Program

SleepinPython_3New

Here, in the beginning, only the first statement is executed. The output of the second print command will be shown after a wait of 20 seconds, as shown below:

/SleepinPython_4

Using sleep() to Delay the Execution of a Function

The time.sleep() function can also be used to delay the execution of a function when it is called in the program. Here is a program to delay the execution of a function.

SleepinPython_5

The program here has a function add() which will show the output “Execution of function add() will be delayed by 20 sec” when it is called. As, we can see here that when the function is called, it does not print the statement because the function’s execution is paused for 10 sec.

After 10 seconds, the output of the program will be as given below:

SleepinPython_6

Now that the execution of  sleep() function is complete, the statement passed under add() function call will be executed.

How to Add a Python sleep() call with Decorators

Python decorators are used to add functionality to functions and methods at the time of definition, rather than at run time. A decorator is a function that extends and returns a function.

A simple program that uses decorator is given below.

SleepinPython_7

In the above code, greet() is a decorator and function name() is decorated by it. It wraps the function in another function called inner().

A decorator in python can be used to find the time taken by a particular function for its execution.

This could be done by defining a simple function time.sleep(n), where n will give the number of seconds in which the program has been executed.

Below is an example for finding execution time of a function using time.sleep() function.

SleepinPython_8.

@measure_time will return the time taken by the function myFunction to execute.

Become the Highest Paid Data Science Expert

With Our Best-in-class Data Science ProgramExplore Now
Become the Highest Paid Data Science Expert

How to Add a Python sleep() call with Threads

In many situations, the sleep() function can be used in parallel with threads. This is a critical function of multi-threading. Here's an example that makes use of several threads and includes a pause with a variable delay:

SleepinPython_9

Here, two threads are being used, the first will print each letter of the string and the second thread will print the whole string. As we can see here that the time.sleep() function halts the execution of the current thread only. This means that only the threads are being paused from execution for a particular time period, instead of the whole program. When the first thread is paused, the second thread starts its execution and then the first thread again takes over the control to complete the program.

Looking forward to making a move to the programming field? Take up the Python Training Course and begin your career as a professional Python programmer

And now that you know everything about sleep() in python, you might want to explore more. You can master Python with the Python Certification Course, and scale up your programming career! If you have any doubts about ‘sleep in python, you can comment below and our experts will help you out at the earliest!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449