Python provides the sleep()
method in order to suspend the execution of the current application or script for the specified time. This is also called delaying the execution for the specified time. It is also popular to stop the execution of the calling thread. The sleep() method can be used to sleep or delay for milliseconds, seconds, minutes, hours even days but the provided value should be as the second count. For example, in order to delay 2 minutes, the provided value will be 120 seconds to the time() method.
sleep() Method Syntax
The sleep()
method is provided via the time
module. So in order to use the sleep() method the time module should be imported. The sleep() method has the following syntax.
sleep(TIME)
- TIME is the number of seconds to delay execution.
- The return value of the sleep() method is void which is no return value.
If the current applications is multi-thread other threads execution continue without problem and only the current thread execution stopped for the specified TIME.
Delay/Sleep For Specified Time
The time.sleep()
method can be used to delay or wait for the specified number of seconds. In the following example, we will delay the execution for 5 seconds. But do not forget to import the time module with the “import time” statement.
import time
print ("Current Time Before Delay: " , time.ctime())
# Current Time Before Delay: Tue Oct 20 02:18:58 2020
time.sleep( 5 )
print ("Current Time After Delay: " , time.ctime())
# Current Time After Delay: Tue Oct 20 02:19:03 2020
Delay/Sleep For Specified Time For Milliseconds
You can also delay or wait for execution for milliseconds. 1 second is equal to 1000 milliseconds. By default the sleep() method accepts seconds but to wait milliseconds you should use a floating-point value for the time value. In the following examples, we will express the 500 milliseconds as 0.5 seconds and provide the 0.5 floating-point value into the sleep() method.
import time
# Wait for 0.5 second or 500 millisecond
time.sleep( 0.5 )
# Wait for 0.6 second or 500 millisecond
time.sleep( .6 )
The floating-point values can be also expressed as without the integer part zero value. For example, 0.6 can be expressed as .6 which can be used to sleep for 600 milliseconds.
In the following example, we will delay or sleep the Python script execution for 7.5 seconds which is equal to 7500 milliseconds. We will provide the 7500 milliseconds to the time.sleep() method like below.
import time
# Wait for 7.5 second or 7500 millisecond
time.sleep( 7.5 )
Sleep 0.1 Second
1 second is equal to 1000 milliseconds this equation makes 0.1 second or .1 second equal to 100 milliseconds. At the time.sleep() method accepts the time value as second in order to sleep 0.1 seconds we should provide 0.1 or .1 as the time parameter to the time.sleep() method like below. As 0.1 second is a very short time duration it can not be caught by humans.
import time
# Wait for 0.1 second or 100 millisecond
time.sleep( 0.1 )
# Wait for 0.1 second or 100 millisecond
time.sleep( .1 )
Input Delay Time
If you need user interaction in order to get the delay time you can get input from the user and delay the provided time. We will use the input()
method to read user input and provide the sleep() method.
import time
print("Please provide delay value")
delay = input()
time.sleep( int(delay) )
print("Please provide delay value")
delay = input()
time.sleep( int(delay))