Python sleep() Function – Tutorial with Examples

The sleep() function in Python is a built-in function that suspends the execution of the current thread for a specified number of seconds. It is included in the time module. The sleep() function is used to add delays in a Python program, allowing it to wait for a certain amount of time before continuing the execution of the code. This function is useful for various purposes, including creating pauses between iterations, waiting for a resource to become available, or simulating real-world scenarios in simulations and tests.

Syntax the sleep() Function

The syntax for the sleep() function is as follows:

time.sleep(secs)

where ‘secs’ is the number of seconds the function will wait before continuing the execution of the code. It is important to note that the argument ‘secs’ must be a non-negative number. If a negative value is provided, a ‘ValueError’ will be raised.

Examples of the sleep() Function

You need to import the time module before you can start making use of the sleep() function.

Example 1: Sleep for a Specified Number of Seconds

The following example demonstrates how to use the sleep() function to wait for 2 seconds before printing a message:

import time

print("Start of example 1")
time.sleep(2)
print("End of example 1")

# Output:
# Start of example 1
# End of example 1 (after 2 seconds)

Example 2: Sleep for a Floating-Point Number of Seconds

The sleep() function can also be used with floating-point numbers to specify a fractional number of seconds to wait. The following example demonstrates how to use the sleep() function to wait for 1.5 seconds before printing a message:

import time

print("Start of example 2")
time.sleep(1.5)
print("End of example 2")

# Output:
# Start of example 2
# End of example 2 (after 1.5 seconds)

Example 3: Sleeping for Different Durations in a Loop

The sleep() function can be used in a loop to create a sequence of pauses with different durations. The following example demonstrates how to use the sleep() function to create a loop that waits for different amounts of time before printing a message:

import time

print("Start of example 3")
for i in range(5):
    time.sleep(i)
    print("End of iteration", i)

# Output:
# Start of example 3
# End of iteration 0 (after 0 seconds)
# End of iteration 1 (after 1 second)
# End of iteration 2 (after 2 seconds)
# End of iteration 3 (after 3 seconds)
# End of iteration 4 (after 4 seconds)

Example 4: Sleeping for a Random Duration

The sleep() function can be used in combination with the ‘random’ module to create pauses with random durations. The following example demonstrates how to use the sleep() function to wait for a random number of seconds between 1 and 5 before printing a message:

import time
import random

print("Start of example 4")
for i in range(5):
    wait_time = random.uniform(1, 5)
    time.sleep(wait_time)
    print("End of iteration", i, "Waited for", wait_time, "seconds")

# Output:
# Start of example 4
# End of iteration 0 Waited for 1.97 seconds
# End of iteration 1 Waited for 3.22 seconds
# End of iteration 2 Waited for 4.76 seconds
# End of iteration 3 Waited for 2.62 seconds
# End of iteration 4 Waited for 1.57 seconds

Example 5: Using sleep() with Multithreading

The sleep() function can also be used in conjunction with multithreading to create multiple threads that run concurrently. The following example demonstrates how to create two threads that run simultaneously and each uses the sleep() function to wait for a different amount of time before printing a message:

import time
import threading

def worker(num):
    print("Thread", num, "start")
    time.sleep(2)
    print("Thread", num, "end")

print("Start of example 5")
threads = []
for i in range(2):
    t = threading.Thread(target=worker, args=(i,))
    threads.append(t)
    t.start()

# Wait for all threads to complete
for t in threads:
    t.join()
print("End of example 5")

Output:

Start of example 5
Thread 0 start
Thread 1 start
Thread 0 end
Thread 1 end
End of example 5

In this example, two threads are created and started using the ‘threading.Thread’ class. Each thread calls the ‘worker’ function, which uses the sleep() function to wait for 2 seconds before printing a message. The ‘join’ method is used to wait for all threads to complete before printing the final message.

Conclusion

In this article, we have discussed the basics of the sleep() function in Python and provided several examples to illustrate its use. The sleep() function is a simple but powerful tool for controlling the timing and flow of a Python program. Whether you are creating a simulation, waiting for a resource to become available, or simply adding pauses between iterations, the sleep() function is a valuable tool to have in your Python programming toolkit.

Leave a Reply

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