Python Date and time

Python sleep() Function (With Examples)

Python sleep() function provides an accurate and flexible way to suspend (wait) execution of the current thread for any period of time. It is defined in terms of a time() module that provides several helpful functions to handle time-related tasks. At times, there is a requirement of keeping the flow of the program on halt so that several other executions can take place.

python time sleep, python sleep function

Python has built-in support that let your program to sleep. One such function is sleep() function.

Python time sleep Examples

Example 1: Python sleep

                    

# Python code to demonstrate working of sleep() function

import time

# Printing the start time of the code

print("Time when code execution begin is: ", end ="")

print(time.ctime())

# using sleep() to halt the code execution for 5 seconds

time.sleep(5)

# printing the end time after using sleep() function

print("Time when code execution end is : ", end ="")

print(time.ctime())

Output

The time, when code execution begins, is: Mon Sep 27 22:57:16 2021

The time when code execution end is: Mon Sep 27 22:57:21 2021

Python sleep function

In the above example, the first execution time is printed immediately when the run button is pressed. When we used the time.sleep() method, suspends a delay of 5 seconds and therefore when we printed the time after the halt, it printed a time of 5 seconds later. The sleep() function can also take floating-point numbers as their arguments.

Example 2: Python create a digital clock

                    

import time

counter = 0

while counter < 10:

localtime = time.localtime()

time_at_the_moment = time.strftime(“%I :%M :%S %p”, localtime)

print(“Time:”, time_at_the_moment)

time.sleep(1)

counter += 1

Output

Time: 11 :17 :33 PM

Time: 11 :17 :34 PM

Time: 11 :17 :35 PM

Time: 11 :17 :36 PM

Time: 11 :17 :37 PM

Time: 11 :17 :38 PM

Time: 11 :17 :39 PM

Time: 11 :17 :40 PM

Time: 11 :17 :41 PM

Time: 11 :17 :42 PM

Sleep in python

In the above code, we computed and printed the current local time inside the while loop 10 times. After that, the program waits for one second and again the current local time is computed and printed and this process continues 10 times. To create a digital clock we can make the while loop infinite and then every second time will be printed.

Python threading sleep

Multithreading is a way of achieving multitasking and it uses the concept of threads. A computer program is a collection of instructions and the execution of these instructions is a process. A thread is a subset of a process, a process can have multiple threads.

                    

# Python program to illustrate the concept of threading importing the threading module

import threading

def print_cube(num):

# Function to print cube of given number

print(“Cube: {}”.format(num * num * num))

def print_square(num):

# Function to print square of given number

print(“Square: {}”.format(num * num))

if __name__ == “__main__”:

# creating threads t1 and t2

t1 = threading.Thread(target=print_square, args=(5,))

t2 = threading.Thread(target=print_cube, args=(5,))

# starting thread-1

t1.start()

# starting thread-2

t2.start()

# this is to wait until thread-1 is completely executed

t1.join()

# this is to wait until thread-2 is completely executed

t2.join()

print(“Both threads are completely executed !”)

Output

Square: 25

Cube: 125

Both threads are completely executed!

Python thread sleep

The time.sleep() function takes time as its argument and suspends the execution of the current thread for the number of seconds specified in the argument. In multi-threaded programs, the function suspends a thread rather than the whole process.

Example: The program shown below has two threads and we have used time.sleep(1) and time.sleep(2) to suspend execution of these two threads for 1 and 2 seconds respectively.

                    

import threading

import time

def say_Hi():

for i in range(3):

time.sleep(1)

print(“Hi”)

def say_Apoyo():

for i in range(3):

time.sleep(2)

print(“Apoyo”)

t1 = threading.Thread(target=say_Hi)

t2 = threading.Thread(target=say_Apoyo)

t1.start()

t2.start()

Output

Hi

Apoyo

Hi

Hi

Apoyo

Apoyo

So we can see in the above code since we suspended the printing of Apoyo for 2 seconds so Hi got printed first and it printed twice as it has a sleep time of only one second. So this way the time.sleep() function can be used to suspend a particular thread for the given number of seconds.

FAQs on Python sleep

Q.1. How many hours did the python sleep?

Answer: Python has a time module that supports a sleep() function, we can pass any time to the argument of this function in seconds and our program will suspend for that much given time.

Q.2. What is Python sleep?

Answer: The python sleep() function provides an accurate and flexible way to suspend the execution of the current thread for any period of time. It is defined in terms of a time() module that provides several helpful functions to handle time-related tasks.

The syntax for using the sleep() function is time.sleep(sec) where sec stands for the number of seconds code is required to be stopped.

Q.3. How do I put Python to sleep?

Answer: You can put python to sleep by using the sleep() function of the time module. For example:

                    

# Python code to demonstrate the working of sleep()

import time

# Printing the start time of the code

print(“Time when code execution begin is: “, end =””)

print(time.ctime())

# using sleep() to halt the code execution for 5 seconds

time.sleep(5)

# printing the end time after using sleep() function

print(“Time when code execution end is : “, end =””)

print(time.ctime())

Output

The time, when code execution begins, is: Mon Sep 27 22:57:16 2021

The time when code execution end is: Mon Sep 27 22:57:21 2021

Q.4. How do you wait 1 second in Python?

Answer: We need to pass 1 to the argument of time.sleep() function and call it right before that section of code where the wait has to be created. For example, in the code shown below, we are creating a 1-second wait and printing the current time to check if the delay is created successfully or not.

                    

import time

counter = 0

while counter < 10:

localtime = time.localtime()

time_at_the_moment = time.strftime(“%I :%M :%S %p”, localtime)

print(“Time:”, time_at_the_moment)

time.sleep(1)

counter += 1

Output

Time: 11 :17 :33 PM

Time: 11 :17 :34 PM

Time: 11 :17 :35 PM

Time: 11 :17 :36 PM

Time: 11 :17 :37 PM

Time: 11 :17 :38 PM

Time: 11 :17 :39 PM

Time: 11 :17 :40 PM

Time: 11 :17 :41 PM

Time: 11 :17 :42 PM

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.