sleep python method pauses/suspends the python program for certain amount of specified time.In other words, sleep method suspends the execution of the calling thread for a given number of seconds.

The method argument can be a floating point to specify a more precise sleep time.Also note that, the actual suspension time might be less than the requested time. Its because of any caught signal which will terminate the sleep() following execution.

Python time.sleep( ): time sleep method is similar to shell sleep command. It accepts the argument in seconds/milli seconds.

For precise sleep times, floating point number can be provided as the argument for python time sleep() method.

Note: time sleep method is part of time module/package in python 2 and python 3, so you need to import the “time” package in the respective python script or function.

Since python version 3.5, the sleep function now sleeps atleast seconds eventhough if the sleep is interrupted by a signal, except incase of signal handler raises an exception via script.

Syntax of Python time.sleep( ):

Following is the syntax for sleep method

time.sleep( t )

Arguments:

t – Number of seconds/milliseconds execution to be suspended in python code.

Example 1:

Following example program illustrates the exact usage of time sleep( ) method in the python script or function.

import time 
# Print the start time  
print("current time start: ", end ="") 
print(time.ctime()) 
  
# using sleep() to pause the code execution 
time.sleep(5) 
  
# Print the end time  
print("current time end: ", end ="") 
print(time.ctime()) 

Output:

current time start: Wed May 29 16:50:48 2019
current time end: Wed May 29 16:50:53 2019

As per the program execution, the wait time or delay is 5 seconds.

Example 2: Delay in Python script

Create a script called sleep-demo.py:

#!/usr/bin/python
# The program will run infinity times on screen till user hit the CTRL+C
# The program will sleep for 10 seconds before updating date and time again.
 
import time
print "!!! Hit CTRL+C to stop the program !!!"
## Start loop ##
while True:
        ### Show today's date and time ##
	print "Current date & time " + time.strftime("%c")
        #### Delay for 10 seconds ####
        time.sleep(10)

Save, close the file and run as follows:
$ chmod +x sleep-demo.py
$ ./sleep-demo.py

Output:

Current date & time Wed May 29 17:50:48 2019
Current date & time Wed May 29 17:50:58 2019
Current date & time Wed May 29 17:51:08 2019
Current date & time Wed May 29 17:51:18 2019
Current date & time Wed May 29 17:51:28 2019
Current date & time Wed May 29 17:51:38 2019

The output will be infinite loop till user interrupts(CTRL+C).

Example 3: Python Multithreading

Python supports multithreading too, here’s an example of a multithreaded Python program.

Create a script called multithread-demo.py:

#!/usr/bin/python

    import threading 
    import time
      
    def print_welcome():
      for i in range(3):
        time.sleep(0.6)
        print("Welcome")
      
    def print_python(): 
        for i in range(3): 
          time.sleep(0.9)
          print("Python") 
    t1 = threading.Thread(target=print_welcome)  
    t2 = threading.Thread(target=print_python)  
    t1.start()
    t2.start()

Output:
As per the above program, execution suspended for two threads at 0.6 seconds and 0.9 seconds respectively.
So start exploring python sleep function in your python script, Happy scripting 🙂

Pin It on Pinterest

Share This