We might want our program or one of its components to run after a certain amount of time. This is made simple using the time.sleep() method. This tutorial explains how to use this function and what it can do.

Why use Python time.sleep()?

When we wish to suspend the execution of a program and let additional executions take place, the sleep() function is really useful. This function is present in both Python versions. It’s a part of Python’s time module. This effectively delays execution and suspends only the current thread, not the entire program.

In the Python time module, there is a method called time.sleep(). Before using it, we must first import it.

import time

After importing this module, we can use the time.sleep() function.



As you can see, it takes one argument: seconds. This results in a delay of a certain number of seconds during execution. This function’s returns void.

Let’s print two different messages in the gap of one second:

import time



print('Hello after one second')



Hello after one second

If the above code is run, the program will be delayed, and the following statement will be executed in one second. For more exact delay, you may also supply floating-point values to the method. If 0.1 seconds have gone, for example, the delay will be 100 milliseconds.

sleep() becomes particularly significant in a multithreaded system since it might introduce latency to the currently executing thread at runtime.


Leave A Comment