Python Sleep() Function

The sleep() function in Python is part of the built-in time module which is used to suspend the execution of the current thread for a specified number of seconds (for example 1, 5, 10, or any number of seconds). The time module in Python provides a range of functions to handle time-related tasks, and the sleep() function is one of the most commonly used methods.

In this article, I will explain the python sleep() function and how to suspend execution for a given number of seconds.

1. Syntax of sleep() Function

Following is a syntax of the sleep() function.


# Syntax of sleep() function
sleep(seconds)

1.1 Parameters of sleep()

  • seconds – The number of seconds to pause the execution of the program. This value can be an integer or a floating-point number.

2. Create a Time Delay in Seconds

You can create a time delay in seconds using the sleep() function from the Python time module. The sleep() function takes a single argument, which is the number of seconds to delay the execution of the program. Below is an example.

Here, It prints "Start" and then waits for 10 seconds before printing “End“. To use this make sure you import the time module.


# Import time
import time

# Create a time delay in seconds
print("Start time of execution :", time.ctime())
time.sleep(10)
print("End time of execution :", time.ctime())

# Output:
# Start time of execution : Thu Jan 19 15:07:04 2023
# End time of execution : Thu Jan 19 15:07:14 2023

Alternatively, you can also import as below and use the sleep() without specifying a time. This will print "Start" and then wait for 10 seconds before printing "End".


# Import sleep from time
from time import sleep

# Create a time delay in seconds
print("Start")
sleep(10)
print("End")

# Output
# Start
# End

You can also use seconds as a floating-point number if you want to delay for a fraction of a second. The below example sleeps for 0.8 seconds or 800 milliseconds. Note that 1 Second is equal to 1000 milliseconds.

Related: How to Sleep Execution for milliseconds in Python


from time import sleep

# Use sleep() function to floating-point
print("Start")
sleep(0.8)
print("End")

# Output
# Start
# End

3. Create a Time Delay in Minutes

You can use the time module to create a time delay in minutes. The sleep() function is used to pause the execution of the program for a specified number of seconds. In this case, 4 minutes are equivalent to 4*60 seconds, so the delay is set to 240 seconds.


from time import sleep
 
# Sleep for 4 minutes
print("Start")
sleep(4*60)
print("End")

# Output
# Start
# End

4. Pause Execution For Every Iteration

In the below example, I am using different methods from the time module. For example, localtime() to get the current system time and strftime() to format the time and of course sleep() to pause the execution for a few seconds.

The below example prints the current time every second for 5 sec, it sleeps for 1 sec for every iteration.


# Import
import time

# Iterate for 5 time and display time
for x in range(0,5):
    currenttime = time.localtime()
    result = time.strftime("%I:%M:%S %p", currenttime)
    print("Current Time: "+result)
    time.sleep(1)

# Output:
# Current Time: 10:48:24 AM
# Current Time: 10:48:25 AM
# Current Time: 10:48:26 AM
# Current Time: 10:48:27 AM
# - - - - - - - - - - - - - 
# - - - - - - - - - - - - -

5. sleep() Function with Multithreading

time.sleep() function is mostly used when you are working with multi-threading in Python. All the above examples I have explained works with a single thread. Let’s see an example of sleep() with multithreaded.


# Import
import threading 
import time

# display_time1()
def display_time1(): 
    for i in range(5): 
        currenttime = time.localtime()
        result = time.strftime("%I:%M:%S %p", currenttime)
        print("Thread 1:",result)
        time.sleep(0.2)
        
# display_time2()        
def display_time2(): 
    for i in range(5): 
        currenttime = time.localtime()
        result = time.strftime("%I:%M:%S %p", currenttime)
        print("Thread 2:", result)
        time.sleep(0.5)
        
# Create threads    
thread1 = threading.Thread(target=display_time1)
thread2 = threading.Thread(target=display_time2)  

# Start threads
thread1.start()
thread2.start()

Yields below output. This example has two threads. We have used time.sleep(0.5) and time.sleep(0.2) to pause the execution of these two threads.

python sleep function

6. Use Time Delay in a List Comprehension

In a list comprehension, the time delay will depend on the size of the iterable and the complexity of the operation being performed on each element. A larger iterable or a more complex operation will result in a longer time delay. The following example sleeps for 5 seconds before print each element.


import time
 
# Use time delay in a list comprehension
technology = ['Java', 'Spark', 'Python', 'Pyspark', 'Hadoop']
technology = [(time.sleep(5), print(tech)) for tech in technology]

# Output
# Java
# Spark
# Python
# Pyspark
# Hadoop

7. Use sleep() Multiple Times

Similarly, you can create multiple time delays in a script by using multiple time.sleep() function calls, each with a different delay duration. Here is an example of how you can create multiple time delays in a script.


import time
   
# creating a time delay of 6 seconds
technology = ['Java', 'Spark', 'Python', 'Pyspark']
time.sleep(2)
print(technology)

# Create time delay in python list
for item in technology:
    time.sleep(5)
    print(item)

# Output
# After the delay of 6 seconds, the list will be display
# ['Java', 'Spark', 'Python', 'Pyspark']

# Then after every 8 seconds, the items on the list will be display
# Java
# Spark
# Python
# Pyspark

Conclusion

In this article, I have explained the python sleep() function suspends execution for a given number of seconds with examples. We have seen how to sleep the program for 10 seconds in the first example and printed the current time every second for 5 sec by sleeping 1 sec.

Happy Learning !!

Leave a Reply

You are currently viewing Python Sleep() Function