Python time sleep | Delay in execution of a program
Today we will discuss the sleep() function present in Python. Sleep() function in Python is used to bring a delay in the execution of a program for a required time. The sleep function is a part of the time module and hence to access this function, we need to import the time module. It takes the number of seconds as an argument to the function and halts the execution for the given number of seconds.
The important point about the sleep function is that it stops the execution of only the current thread rather than the whole program. A simple example using this function is:
import time time.sleep(2) print ("delay created")
In the above code, we import the time module to access the sleep function. Here, we have passed 2 seconds as the argument to the sleep function. Hence when the code is run in the python console, it is observed that the print statement is executed after a delay of 2 seconds.
With the help of sleep function, we can even have different delay times in a single program like,
import time l=['Good', 'Morning', 'Everyone'] for i in [0, 1, 2]: time.sleep(i) print("delay of ",i, "seconds") print(l[i])
delay of 0 seconds Good delay of 1 seconds Morning delay of 2 seconds Everyone
Also read: Partial Functions in Python
Sleep function for a multithreaded program in Python
A thread is the smallest unit within an OS and is a part of the process( Process refers to the execution of the collection of instructions). When more than one thread is present inside a process, the program is referred to as a multithreaded program. In a multithreaded program, the sleep function suspends the execution of only the current thread, unlike the single-threaded programs where it suspends the thread along with the process. Here is an example of a multithreaded program with sleep function.
import time from threading import Thread class batch(Thread): def run(self): for i in range(0, 10): print(i) time.sleep(1) class number(Thread): def run(self): for j in range(50, 53): print(j) time.sleep(3) print("Start the First Thread: batch") batch().start() print("Start the Second Thread: number") number().start()
Start the First Thread: batch 0 Start the Second Thread: number 50 1 2 3 51 4 5 6 52 7 8 9
In the above code whenever the sleep function is called, it suspends the execution of the current thread and not the entire program.
How does sleep() functionality work?
The sleep function uses a system clock that emits high-frequency signals. These signals synchronize all the internal components of the computer. This function generates an interrupt request that interrupts the execution of the current code. It saves the current state of the code and then calls the asynchronous procedure call that blocks the execution. The OS stops the process for a particular period. Once this time is elapsed the execution of the process resumes from the very last state.
The sleep function can be used for several reasons like,
- For dramatic printing, the example for this is shown below:
import time string = " SCARY" for i in string: time.sleep(2) print(i)
This code brings about suspense and creates a dramatic effect by printing each character one by one. Note that the visual effect for the above code can be seen only in the editor window.
- To design some basic and simple tools like the digital clock
- To halt the execution of a program during uploading or downloading a file.
- To wait for a graphic to get loaded and drawn on the screen.
- To check the status of a website or a user interface during an automated test.
- To add delays in between downloading millions of files within a GUI in order to avoid a server from slowing down.